From 12c185ca49cd84ffbbb54a25d150ffc5221afe8e Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Mon, 9 May 2016 20:57:23 +0300 Subject: [PATCH] Fix for regex issue with product autocomplete --- public/built.js | 5 ++++- public/js/script.js | 6 +++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/public/built.js b/public/built.js index 722f11a301a6..8cd3a1e70e03 100644 --- a/public/built.js +++ b/public/built.js @@ -30991,7 +30991,7 @@ function searchData(data, key, fuzzy) { matches = fuse.search(q); } else { matches = []; - substrRegex = new RegExp(q, 'i'); + substrRegex = new RegExp(escapeRegExp(q), 'i'); $.each(data, function(i, obj) { if (substrRegex.test(obj[key])) { matches.push(obj); @@ -31002,6 +31002,9 @@ function searchData(data, key, fuzzy) { } }; +function escapeRegExp(str) { + return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); +} var NINJA = NINJA || {}; NINJA.TEMPLATES = { diff --git a/public/js/script.js b/public/js/script.js index 8b53e500ef64..a76e6d37cd0e 100644 --- a/public/js/script.js +++ b/public/js/script.js @@ -1097,7 +1097,7 @@ function searchData(data, key, fuzzy) { matches = fuse.search(q); } else { matches = []; - substrRegex = new RegExp(q, 'i'); + substrRegex = new RegExp(escapeRegExp(q), 'i'); $.each(data, function(i, obj) { if (substrRegex.test(obj[key])) { matches.push(obj); @@ -1107,3 +1107,7 @@ function searchData(data, key, fuzzy) { cb(matches); } }; + +function escapeRegExp(str) { + return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); +} \ No newline at end of file