How does JavaScript detect regular expressions?

2019-04-21 00:07发布

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?

1条回答
我想做一个坏孩纸
2楼-- · 2019-04-21 01:10

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.

查看更多
登录 后发表回答