Reputation: 432
Example taken from MDN webdocs
var paragraph = 'The quick brown fox jumps over the lazy dog. It barked.';
var regex = /[A-Z]/g;
var found = paragraph.match(regex);
console.log(found); // will return an array of matches and returns null when nothing matches.
I would like to know the reasoning behind returning null instead of returning an Empty Array when nothing matches.
Upvotes: 5
Views: 1623
Reputation: 254916
That's how String.prototype.match
is defined in the EcmaScript standard
In short: if nothing matches - it returns null
by the standard.
Upvotes: 2