Crockford第7章 - make_a_matcher

时间:2013-10-13 10:27:12

标签: javascript regex

在第72页的书中,我们有这个例子(见下文)。但是当我在FF中测试它时,在NodeJS中,实际y.lastIndex0,而x === yfalse

知道为什么书中的信息和实际行为之间存在这种不匹配的原因?

function make_a_matcher() {
    return /a/gi;
}
var x = make_a_matcher();
var y = make_a_matcher();
// Beware: x and y are the same object!
x.lastIndex = 10;
document.writeln(y.lastIndex); // 10

1 个答案:

答案 0 :(得分:3)

这很奇怪,但我认为这是一个错误,除非旧的Javascript解释器缓存正则表达式文字并返回相同的实例。

正如@fgb在评论中提到的The ES3 standard allowed interpreters to cache the literals, but this was restricted in ES5,再次感谢@fgb。

无论如何,它都是Unconfirmed Errata