转自:http://bbs.blueidea.com/thread-2704718-1-1.html
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
+++++++++++++
不是reg.test改变了str,而是reg.test改变了reg的内部状态
像这样的:
alert(/\w*\[[^:]*\]\w*/g.test(str));
alert(/\w*\[[^:]*\]\w*/g.test(str));
alert(/\w*\[[^:]*\]\w*/g.test(str));
alert(/\w*\[[^:]*\]\w*/g.test(str));
就一直会有确定的结果,因为每一句里的正则表达式对象都是独立的
虽然没看到有正式的资料,但我估计是这样的:
加了全局标志的正则对象在test时会记录成功匹配的位置,下次调用test,会从该位置继续
下面两段代码的执行结果可以证明这一点:
提示:您可以先修改部分代码再运行
提示:您可以先修改部分代码再运行