How to compare two regexps?
Try this:
String(regexp1) === String(regexp2))
You are getting false because those two are different objects.
"Problem":
regex
is an object
- a reference type, so the comparsion is done by reference, and those are two different objects.
console.log(typeof /a/); // "object"
If both operands are objects, then JavaScript compares internal references which are equal when operands refer to the same object in memory.
MDN
Solution:
var a = /a/;
var b = /a/;
console.log(a.toString() === b.toString()); // true! yessss!
Live DEMO
Another "hack" to force the toString()
on the regex
es is:
console.log(a + "" === b + "");
Just a guess - but doesn't JavaScript create a RegExp
object for your regex, and therefore because you have created two different objects (even though they have the same "value") they're actually different?