What you have will work as long as you first escape the regex characters in your pattern. Below is one example from Simon Willison’s blog:
RegExp.escape = function(text) {
if (!arguments.callee.sRE) {
var specials = [
"https://stackoverflow.com/", '.', '*', '+', '?', '|',
'(', ')', '[', ']', '{', '}', '\\'
];
arguments.callee.sRE = new RegExp(
'(\\' + specials.join('|\\') + ')', 'g'
);
}
return text.replace(arguments.callee.sRE, '\\$1');
}
You could then implement your code as:
likeExpr = RegExp.escape(likeExpr);
var match = new RegEx(likeExpr.replace("%", ".*").replace("_", ".")).exec(str) != null;