Why does Javascript's regex.exec() not always return the same value? [duplicate]

问题:

This question already has an answer here: 这个问题在这里已有答案:

In the Chrome or Firebug console: 在Chrome或Firebug控制台中:

reg = /ab/g
str = "abc"
reg.exec(str)
   ==> ["ab"]
reg.exec(str)
   ==> null
reg.exec(str)
   ==> ["ab"]
reg.exec(str)
   ==> null

Is exec somehow stateful and depends on what it returned the previous time? exec在某种程度上是有状态的,取决于它之前返回的内容吗? Or is this just a bug? 或者这只是一个错误? I can't get it to happen all the time. 我无法让它一直发生。 For example, if 'str' above were "abc abc" it doesn't happen. 例如,如果上面的'str'是“abc abc”,则不会发生。


解决方案:

参考: https://stackoom.com/en/question/m9nb
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章