JavaScript如何检测正则表达式?

时间:2021-06-22 15:44:53

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?

我正在编写一个JS解析器,我想知道如何区分正则表达式(/lookup/g)和简单除法(bar/baz/g)。JavaScript用来识别正则表达式的规则是什么?

1 个解决方案

#1


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).

您希望查看ECMA规范中的7.8.5节(注释版本目前是最新的,但总是检查ECMA的最新PDF)。

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.

还要记住,JavaScript regex不能为空。//总是一行注释的开始。值得一提的是,在正则表达式之前决不能插入分号。

Tangential, an empty JavaScript regex looks like /(?:)/.

切向,一个空的JavaScript正则表达式看起来像/(?:)/。

Further discussion.

进一步讨论。

#1


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).

您希望查看ECMA规范中的7.8.5节(注释版本目前是最新的,但总是检查ECMA的最新PDF)。

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.

还要记住,JavaScript regex不能为空。//总是一行注释的开始。值得一提的是,在正则表达式之前决不能插入分号。

Tangential, an empty JavaScript regex looks like /(?:)/.

切向,一个空的JavaScript正则表达式看起来像/(?:)/。

Further discussion.

进一步讨论。