admin管理员组文章数量:1428488
If JavaScript, /\.scss$/
is a regex. I want to check whether a regex is exactly the same as another regex, however:
/\.scss$/ === /\.scss$/ // false
How can I do this? Note that I do not care about what the regex matches, I only care about the way the regex is defined.
If JavaScript, /\.scss$/
is a regex. I want to check whether a regex is exactly the same as another regex, however:
/\.scss$/ === /\.scss$/ // false
How can I do this? Note that I do not care about what the regex matches, I only care about the way the regex is defined.
Share Improve this question edited Mar 31, 2016 at 8:55 sennett asked Mar 31, 2016 at 8:42 sennettsennett 8,45411 gold badges49 silver badges72 bronze badges 1-
typeof /\.scss$/
is"object"
and two objects cannot be pared using equality operators. – Tushar Commented Mar 31, 2016 at 8:53
2 Answers
Reset to default 7Use .toString
:
/\.scss$/.toString() === /\.scss$/.toString() // true
It's easier to see what is going on when using the RegExp object:
new RegExp("ab+c").toString() === new RegExp("ab+c").toString() // true
whereas
new RegExp("ab+c") === new RegExp("ab+c") // false
How about using source attribute?
/\.scss$/.source === /\.scss$/.source && /\.scss$/.flags === /\.scss$/.flags
It's faster then using toString function. http://jsperf./source-vs-tostring
本文标签: javascriptHow can I check if two regular expressions are the sameStack Overflow
版权声明:本文标题:javascript - How can I check if two regular expressions are the same? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745509809a2661414.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论