示例取自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.
想知道什么都不匹配时返回null而不是Empty Array的原因吗?
这就是EcmaScript标准中定义String.prototype.match
的方式
简而言之:如果没有匹配项,则按标准返回null
。