I’m writing an app that lets a user specify a regular expression. Of course, users make mistakes, so I need a way to handle regular expressions that are unparseable, and give the user some actionable advice on how to fix the problem.
The problem I’m having is that the exceptions thrown by new RegExp("something awful") are not helpful for regex n00bs, and have different messages per browser. For example:
Given:
try{
new RegExp("(pie");
}catch(e){
console.log(e.message);
}
- Firefox throws “unterminated parenthetical”.
- Safari throws “missing )”
- Chrome throws “Unterminated group”
And it wouldn’t surprise me if those message strings are user-language-localized, or that they’ve drifted over time, making this a crazy knot to untie with exception.message.
My goal is to catch the exception, figure out what it’s really about, and put up a much more beginner-friendly message. (And eventually highlighting the unmatched paren, in this example.)
Is there some other exception identifier I should be using? Is there a better way to tell these apart? Failing all of that, has anyone just collected what all these strings are across the several most popular browsers?
Idea: Figure it all out at runtime. E.g.
Of course, this will only work well if a browser’s in-built error reporting is specific enough. Many of them suck. E.g. Opera gives absolutely no hint as to the issue, so the above won’t work well, and neither will any other solution relying on Opera’s native error messages.
I would suggest sending regexps off to an app running node.js and getting the nice V8 error messages 🙂