Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing for equality of regular expressions

Tags:

javascript

I was surprised to see that

/a/ === /a/

evaluates to false in JavaScript. Reading through the specs:

Two regular expression literals in a program evaluate to regular expression objects that never compare as === to each other even if the two literals' contents are identical.

Since === cannot be used to test for equality, how can equality of regular expressions be tested in JavaScript?

like image 209
Randomblue Avatar asked May 27 '12 19:05

Randomblue


4 Answers

Here's a case that even covers ordering of flags.

function regexEqual(x, y) {
    return (x instanceof RegExp) && (y instanceof RegExp) && 
           (x.source === y.source) && (x.global === y.global) && 
           (x.ignoreCase === y.ignoreCase) && (x.multiline === y.multiline);
}

Tests:

regexEqual(/a/, /a/) // true
regexEqual(/a/gi, /a/ig) // also true.
regeXEqual(/a/, /b/) // false
like image 185
Arka Avatar answered Oct 15 '22 16:10

Arka


Here's a function that fully tests all the relevant regex properties and makes sure it's the right type of object:

function regexSame(r1, r2) {
    if (r1 instanceof RegExp && r2 instanceof RegExp) {
        var props = ["global", "multiline", "ignoreCase", "source", "dotAll", "sticky", "unicode"];
        for (var i = 0; i < props.length; i++) {
            var prop = props[i];
            if (r1[prop] !== r2[prop]) {
                return false;
            }
        }
        return true;
    }
    return false;
}

And, since flags sometimes get added to the regex object with new features (as has happened since this original answer in 2012 - though the above code has been updated as of 2019), here's a version that is a bit more future proof on future flags being added since it compares whatever flags are there rather than looking for a specific set of flags. It sorts the flags before comparing to allow for minor differences in how the regex was specified that wouldn't not actually change functionality.

function regexSame(r1, r2) {
    return r1 instanceof RegExp && 
           r2 instanceof RegExp &&
           r1.source === r2.source &&
           r1.flags.split("").sort().join("") === r2.flags.split("").sort().join("");
}
like image 25
jfriend00 Avatar answered Oct 15 '22 15:10

jfriend00


Compare them using toString(), and check their type too:

var a = /a/,
    b = /a/;

a.toString() === b.toString() && typeof(a) === typeof(b)  //true

var c = /a/,
    d = /b/;

c.toString() === d.toString() && typeof(c) === typeof(d)  //false
like image 22
Derek 朕會功夫 Avatar answered Oct 15 '22 17:10

Derek 朕會功夫


You can check the types with typeof, then toString() both regexes and compare those. It won't cover cases with equivalent flags, such as /a/gi and /a/ig, though.

function regexEquals(a, b)
{
    if (typeof a !== 'object' || typeof b !== 'object') return false;

    return a.toString() === b.toString();
}

Unfortunately there's no more-specific type from typeof, so if you really want to make sure they're regexes (or regex-like) you could do something along these lines:

RegExp.prototype.regexEquals = function (other)
{
    return (typeof other.regexEquals === 'function')
        && (this.toString() === other.toString());
}

Then:

/a/.regexEquals(/a/); // true
/a/.regexEquals(/b/); // false
like image 27
Matt Ball Avatar answered Oct 15 '22 15:10

Matt Ball