42

I'm just a noob when it comes to regexp. I know Perl is amazing with regexp and I don't know much Perl. Recently started learning JavaScript and came across regex for validating user inputs... haven't used them much.

How does JavaScript regexp compare with Perl regexp? Similarities and differences?
Can all regexp(s) written in JS be used in Perl and vice-versa?
Similar syntax?

1
  • 1
    I'm curious to see if anyone answers this. I know the basic regex feature are the same between Perl and JavaScript (^ anchors left, $ anchors right, \b for word boundary, etc). Some of the advanced feature may differ though, like non-greedy matching and back references. Commented Oct 16, 2010 at 17:56

5 Answers 5

32

From ECMAScript 2018 onwards, many of JavaScript's regex deficiencies have been fixed.

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.

Sign up to request clarification or add additional context in comments.

2 Comments

You can fake verbose regexs with string concatenation. It's not as nice as true verbose regexes, so the barrier is higher for when you would do it, but it's still an option and is definitely worthwhile in some situations.
Lookbehind assertions have been added in ECMAScript 2018.
8

This comparison will answer all your queries.

4 Comments

Except it's about three years out of date, since it only covers Perl 5.8, and several of the things on that list were added in 5.10.
@hobbs - some of us still live in the 5.8 world :)
To be specific (that is, actually helpful), 5.10 adds named captures, named backreferences, and the "possessive quantifiers" using +, as well as some other stuff related to backtracking control and recursive-descent parsing that isn't in that comparison.
I cannot see any reference to either Ecmascript or javascript.
6

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.

2 Comments

or use the character class [^]
The s modifier has been added in ECMAScript 2018.
0

I just ran into an instance where the \d, decimal is not recognized in some versions of JavaScript -- you have to use [0-9].

Comments

0

Something that bit me in the butt: most flavors don't allow empty character classes/sets, so the ] is treated as literal if the first character.

EcmaScript allows empty character classes/sets, which just don't match anything and are fully ignored.

[][!] from man glob:

  • PCRE, Python, .NET - equivalent to [\[\]!], matches any of [, ], !
  • EcmaScript - simplifies to just [!], matches only ! but not [, ]
  • Java, Rust - invalid, does not compile

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.