Reputation: 2044
I know there are a few questions regarding this topic, but for all of them I saw a specific regular expression to be converted from Java to JavaScript.
However, non of the answers contained some kind of an algorithm to do the conversion in a generic way. So my question is if you know about an algorithm to convert Java regular expression to JavaScript regular expression? perhaps a third-party to do so?
Motivation and background: in client side, I get list of attributes from the server to render a form. Some of the form fields contain validation patterns to be checked in server later. For usability purposes, I want to be able to present the user the validation inline (like validation of an IP address), without maintaining regular expressions both in server and client.
Cheers,
Upvotes: 2
Views: 1344
Reputation: 51330
The syntax is generally the same, but JavaScript has much less support for regex features.
For instance, lookbehinds are not supported, neither are possessive quantifiers, Unicode categories, named groups, the s
option etc.
You can see a comprehensive feature comparison on this site, but in the end JavaScript regexes are more or less a subset of Java regexes.
What you can do is write JS-compatible regexes and use them in Java, but if your Java regexes use features unavailable in JS you're out of luck.
You could also use the XRegExp library in JS to expand somewhat on the available features. It's a lib that converts some unsupported syntax to supported syntax, but it can't provide all the missing features.
Upvotes: 1