I am writing an application in javascript. In my application there is an option to search for a string/regex. The problem is match returns javascript error if user types wrong value. Sample code:
function myFunction() {
var filter = $("#text_id").val();
var query = "select * from table";
var found;
if (query.match(filter) != -1) {
found = true;
}
else{
found = false;
}
//Do something
}
jsfiddle: http://jsfiddle.net/ZVNMq/
Enter the string: sel/\
Match returns js error - Uncaught SyntaxError: Invalid regular expression: /sel\/: \ at end of pattern.
Is there any way to check whether the string is valid regex or not?
try catchis the fastest and painless way.