Javascript regex compared to Perl regex
From ECMAScript 2018 onwards, many of JavaScript's regex deficiencies have been fixed.
- It now supports lookbehind assertions, even unbounded ones.
- Unicode property escapes have been added.
- There finally is a DOTALL (
/s
) flag.
What is still missing:
- JavaScript doesn't have a way to prevent backtracking by making matches final (using possessive quantifiers
++
/*+
/?+
or atomic groups(?>...)
). - Recursive/balanced subgroup matching is not supported.
- One other (cosmetic) thing is that JavaScript doesn't know verbose regexes, which might make them harder to read.
Other than that, the basic regex syntax is very similar in both flavors.
Another difference: In JavaScript, there is no s
modifier: The dot "." will never match a newline character. As a replacement for ".", the character class [\s\S]
can be used in JavaScript, which will work like /./s
in Perl.
This comparison
will answer all your queries.