JavaScript如何检测正则表达式? [英] How does JavaScript detect regular expressions?
问题描述
我正在编写一个JS解析器,我想知道如何区分正则表达式( / lookup / g
)和简单划分( bar /巴兹/克
)。 JavaScript用于识别正则表达式的规则是什么?
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?
推荐答案
您想要查看第7.8.5节(带注释的版本目前是最新的,但始终检查来自ECMA的最新PDF )。
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).
请记住,JavaScript正则表达式不能为空。 //
总是单行注释的开头。还值得一提的是,在正则表达式字面值之前绝不能插入分号。
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,空 JavaScript正则表达式看起来像 /(?:) /
。
Tangential, an empty JavaScript regex looks like /(?:)/
.
这篇关于JavaScript如何检测正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!