11

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?

0

1 Answer 1

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 /(?:)/.

Further discussion.

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

4 Comments

Note that the grammar appears to have changed a bit in the latest version.
@AndrewMarshall: I would of linked that if it were in HTML format - do you know if one exists? Thanks.
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.
@alex: Here is the link again. I deleted the comment by accident (don't ask).

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.