4 ms·
I'm not an expert in regexes in other platforms, but already if you want a common subset between Java and JS, you're limited to the following constructs: * Lit
by sjrd 5y ago
I'm not an expert in regexes in other platforms, but already if you want a common subset between Java and JS, you're limited to the following constructs:
* Literal characters
* Some escape sequences for literal characters: \t \r \n \xHH \uUUUU as well as the ones for the regex metacharacters such as \[ \( etc.
* Groups, sequences and alternatives
* Look-ahead (positive and negative)
* Look-behind, if supported (ES 2018+)
* Back-references to existing groups opened before the back-references, but you need to make sure that they're not syntactically followed by a digit, otherwise parsing is different between JS and Java
* Greedy and lazy quantifiers
* Custom character classes with [], without intersections or inner custom character classes
That's assuming you have 'u' support in JS and that you use it (ECMAScript 2015), and that you don't use any other flag. (you can use the 'g' flag in JS or not; it affects the behavior of the methods, but not of the regex itself). If you don't have 'u' support, it's only the same if you restrict your regex and your input to ASCII characters.
Some things that are unexpectedly NOT portable:
* Most pre-defined character classes like \s and the like
* CASE_INSENSITIVE/'i' and MULTILINE/'m'
* \p{...} character classes
* \b and \B (there's a bug in OpenJDK: https://bugs.openjdk.java.net/browse/JDK-8264160 https://bugs.openjdk.java.net/browse/JDK-8264160)
- slimsag 5y agoAlso: * Named groups are not supported everywhere. * Lookahead/lookbehind are not supported in Go and Rust (unless using fancy-regexp, from what I understand) Also beware that character classes like `[]` have differing Unicode support, some operate on ASCII, unicode codepoints, or on extended grapheme clusters.. so YMMV. As you start to expand to more languages, the common feature set is _far_ more limited.
- vbezhenar 5y agoLookbehind is not supported in Safari: https://caniuse.com/?search=lookbehind%20regex https://caniuse.com/?search=lookbehind%20regex
- slimsag 5y agoWow, I didn't know that - surprising!
- ridiculous_fish 5y agoLookbehinds are annoying to implement, because they require driving the character iteration backwards. Now every regex construct has to be parametrized on whether it is tracking forwards or backwards. It is not something which can be easily tacked on to an existing engine. I will plug my own Rust regex engine "regress", which supports arbitrary lookbehinds. Note fancy-regex only supports fixed-width lookbehinds. https://crates.io/crates/regress https://crates.io/crates/regress
- eevilspock 5y agoWow. Thank you for this thorough answer! It will be very helpful for the project I'm working on.