close

如果 Regular Expression 的結尾加了  "g", 那麼每次執行 test 之後, RegExp 會記住前一次執行的結尾位置.

下一次執行 test 時, 不會再從字串的第1個字元, 而是從上述的結尾位置開始查找

結果就會很靈異.

解法:

1) 移除 Regular Expression 的結尾 g

2) 每呼叫1次 test 後, 立即再把RegExp物件的屬性 lastIndex 重設成 0

出處:

https://blog.csdn.net/qq_30100043/article/details/80092012

https://blog.csdn.net/aitangyong/article/details/51470854

https://stackoverflow.com/questions/24084926/javascript-regexp-cant-use-twice

 

筆記.

 

  

 

 

arrow
arrow
    文章標籤
    javascript Regular Expression
    全站熱搜

    汪湯姆 發表在 痞客邦 留言(0) 人氣()