未捕获的SyntaxError:无效的正则表达式:/ [/:未终止的字符类未捕获的SyntaxError:无效的正则表达式:/ [/:未终止的字符类
// 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());
});
});
是的,那是运行时错误。
该搜索()方法需要一个正则表达式作为它的参数,但如果你是传递一个字符串应该逃过正则表达式特殊字符,如^, |
等等等等
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());
});
});
演示:小提琴
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句