I am writing a JS parser, and am wondering how to differentiate between a regular expression (/lookup/g
) and simple division (bar/baz/g
). What are the rules that JavaScript uses to identify regular expressions?
Asked
Active
Viewed 539 times
11

Azmisov
- 6,493
- 7
- 53
- 70
1 Answers
11
You want to check out Section 7.8.5 in the ECMA spec (the annotated version is up-to-date currently, but always check the latest PDF from the ECMA).
Remember too that a JavaScript regex can not be empty. //
is always the start of a single line comment. It's also worth mentioning that a semicolon must never be inserted before a regex literal.
Tangential, an empty JavaScript regex looks like /(?:)/
.

alex
- 479,566
- 201
- 878
- 984
-
Note that the grammar appears to have changed a bit in the [latest version](http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf). – Andrew Marshall Nov 30 '11 at 22:57
-
@AndrewMarshall: I would of linked that if it were in HTML format - do you know if one exists? Thanks. – alex Nov 30 '11 at 22:59
-
Yea that's why I figured you'd linked to that version instead. And no I don't, unfortunately. I just wanted to add the link to the latest version for completeness sake. – Andrew Marshall Nov 30 '11 at 23:01
-
@alex: [Here](http://es5.github.com/#x7.8.5) is the link again. I deleted the comment by accident (don't ask). – kubetz Nov 30 '11 at 23:03