I’m messing around with QUnit and there is one thing I stumbled upon.
I tried this simple test on Chrome:
deepEqual(new RegExp(), /(?:)/);
I assumed it would pass, since new RegExp() returns /(?:)/ in the Developer Console. It seems not possible to ‘just’ do new RegExp() === /(?:)/ for RegExps, but the toString() function of both return the same and are equal.
I thought that the literal/non-literal notation would make a difference, but that cannot be the case since this test passes:
deepEqual(new RegExp(" "), / /);
So, from the following tests the first fails:
test("test", function() {
deepEqual(new RegExp(), /(?:)/); // fail
deepEqual(new RegExp(" "), / /); // pass
equal(new RegExp().toString(), /(?:)/.toString()); // pass
});
Therefore, could someone point me in the right direction as to why the first test fails please?
Short answer: The value of the
sourceproperty is different for the regex-literal/(?:)/and the object you get fromnew RegExp(). In the case of the literal it’s/(?:)/, whereas in the case of the object, it’s an empty string. When you do/ /andnew RegExp(" "), the value of thesourceproperty is the same (both are strings with one space-character).Long answer: If you look at Qunit’s source, you’ll see this bit of code:
You can see how the source parameter is different using this code (it simply outputs the properties of each regex argument and tests them for equality):
When you call this with
eq(/(?:)/, new RegExp());, you get:Whereas when you call it with
eq(/ /, new RegExp(" "));you get: