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