Reputation: 17
Uncaught SyntaxError: Invalid regular expression: /[/: Unterminated character classUncaught SyntaxError: Invalid regular expression: /[/: Unterminated character class
// Searching in ListBox
$('#Search').keyup(function () {
var valThis = $(this).val().toUpperCase();
$('#lstDCU > option').each(function () {
var itemText = $(this).text().toUpperCase();
((itemText.search(valThis) != -1) ? $(this).show() : $(this).hide());
});
});
Upvotes: 1
Views: 2428
Reputation: 388316
Yes, that is a runtime error.
The search() method expects a regex as its parameter, but if you are passing a string it should be escaped for regex special characters like ^, |
etc so
if (!RegExp.escape) {
RegExp.escape = function (value) {
return value.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&")
};
}
$('#Search').keyup(function () {
var valThis = RegExp.escape($(this).val().toUpperCase());
$('#lstDCU > option').each(function () {
var itemText = $(this).text().toUpperCase();
((itemText.search(valThis) != -1) ? $(this).show() : $(this).hide());
});
});
Demo: Fiddle
Upvotes: 3