Sometimes, we want to compare two JavaScript regexes.
In this article, we’ll look at how to compare two JavaScript regexes.
How to compare two JavaScript regexes?
To compare two JavaScript regexes, we can convert JavaScript regex objects to strings before comparing.
For instance, we write:
const regexp1 = /a/;
const regexp2 = /a/;
const isSame = String(regexp1) === String(regexp2)
console.log(isSame)
to convert regexp1
and regexp2
to strings with String
before doing the comparison.
Therefore, isSame
is true
since they have the same content.
Conclusion
To compare two JavaScript regexes, we can convert JavaScript regex objects to strings before comparing.