我在JavaScript中创建了一个RegExp对象,用于测试数字的存在:
var test = new RegExp( '[0-9]', 'g' );
我这样使用它。
console.log( test.test( '0' ) ); // true
console.log( test.test( '1' ) ); // false - why?
这个的输出结果更加混乱:
console.log( test.test( '1' ) ); // true
console.log( test.test( '0' ) ); // false - why?
console.log( test.test( '1' ) ); // true
console.log( test.test( '2' ) ); // false - why?
console.log( test.test( '2' ) ); // true - correct, but why is this one true?
如果我删除
g
限定符,它会按预期工作。这是一个错误吗?还是规范的某个奇怪部分?g
限定符应该这样使用吗?(我正在为多个任务重复使用同一表达式,因此必须使用限定符)