How can I emulate the SQL keyword LIKE
in JavaScript?
For those of you who don\'t know what LIKE
is, it\'s a very simple regex which only s
Here's a function I use, based on PHP's preg_quote function:
function regex_quote(str) {
return str.replace(new RegExp("([\\.\\\\\\+\\*\\?\\[\\^\\]\\$\\(\\)\\{\\}\\=\\!\\<\\>\\|\\:\\-])", "g"), "\\$1");
}
So your line would now be:
var match = new RegEx(regex_quote(likeExpr).replace("%", ".*").replace("_", ".")).exec(str) != null;