How do you check, in JavaScript, if a string is a proper regular expression that will compile?
For example, when you execute the following javascript, it produces a
You can use try/catch and the RegExp constructor:
try/catch
RegExp
var isValid = true; try { new RegExp("the_regex_to_test_goes_here"); } catch(e) { isValid = false; } if(!isValid) alert("Invalid regular expression");