Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check if two regular expressions are the same?

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.

like image 966
sennett Avatar asked Nov 02 '25 02:11

sennett


1 Answers

Use .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
like image 140
sennett Avatar answered Nov 04 '25 20:11

sennett