Skip to content

Commit 839fd1e

Browse files
author
SimonSteinberger
committed
Auto escape Regex characters.
1 parent 71b76d7 commit 839fd1e

File tree

3 files changed

+6
-1
lines changed

3 files changed

+6
-1
lines changed

auto-complete.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ var autoComplete = (function(){
3636
cache: 1,
3737
menuClass: '',
3838
renderItem: function (item, search){
39+
// escape special characters
40+
search = search.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
3941
var re = new RegExp("(" + search.split(' ').join('|') + ")", "gi");
4042
return '<div class="autocomplete-suggestion" data-val="' + item + '">' + item.replace(re, "<b>$1</b>") + '</div>';
4143
},

auto-complete.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demo.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ <h3>Settings</h3>
119119
<td>
120120
<p>A function that gives you control over how suggestions are displayed. Default:</p>
121121
<pre class="prettyprint" style="white-space:pre-wrap"><code>renderItem: function (item, search){
122+
search = search.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&amp;');
122123
var re = new RegExp(&quot;(&quot; + search.split(' ').join('|') + &quot;)&quot;, &quot;gi&quot;);
123124
return '&lt;div class=&quot;autocomplete-suggestion&quot; data-val=&quot;' + item + '&quot;&gt;' + item.replace(re, &quot;&lt;b&gt;$1&lt;/b&gt;&quot;) + '&lt;/div&gt;';
124125
}</code></pre>
@@ -223,6 +224,7 @@ <h4>Advanced suggestions handling and custom layout</h4>
223224
suggest(suggestions);
224225
},
225226
renderItem: function (item, search){
227+
search = search.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&amp;');
226228
var re = new RegExp(&quot;(&quot; + search.split(' ').join('|') + &quot;)&quot;, &quot;gi&quot;);
227229
return '&lt;div class=&quot;autocomplete-suggestion&quot; data-langname=&quot;'+item[0]+'&quot; data-lang=&quot;'+item[1]+'&quot; data-val=&quot;'+search+'&quot;&gt;&lt;img src=&quot;img/'+item[1]+'.png&quot;&gt; '+item[0].replace(re, &quot;&lt;b&gt;$1&lt;/b&gt;&quot;)+'&lt;/div&gt;';
228230
},
@@ -287,6 +289,7 @@ <h4>Advanced suggestions handling and custom layout</h4>
287289
suggest(suggestions);
288290
},
289291
renderItem: function (item, search){
292+
search = search.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&amp;');
290293
var re = new RegExp("(" + search.split(' ').join('|') + ")", "gi");
291294
return '<div class="autocomplete-suggestion" data-langname="'+item[0]+'" data-lang="'+item[1]+'" data-val="'+search+'"><img src="img/'+item[1]+'.png"> '+item[0].replace(re, "<b>$1</b>")+'</div>';
292295
},

0 commit comments

Comments
 (0)