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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章