本文介绍了为什么我的Javascript中的两个正则表达式文字在属性上有所不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在Douglas Crockford的 Javascript:The Good Parts 中读到javascript正则表达式文字共享同一个对象。如果是这样,那么这两个正则表达式文字如何在 lastIndex
属性中变化?
I read in Javascript: The Good Parts by Douglas Crockford that javascript regular expression literals share the same object. If so, then how come these two regex literals vary in the lastIndex
property?
var a = /a/g;
var b = /a/g;
a.lastIndex = 3;
document.write(b.lastIndex);
输出0而不是3。
推荐答案
非常清楚它们是两个不同的对象:
Section 7.8.5 of the ECMAScript Documentation makes it quite clear they are two different objects:
这篇关于为什么我的Javascript中的两个正则表达式文字在属性上有所不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!