Javascript中,正則表達式起用 `g` 標誌位後,這個正則居然是有狀態的

const reg = /^[0-9]+/g
reg.test('123') // expect true actual true
reg.test('123') // expect true actual false
reg.test('123') // expect true actual true
reg.test('123') // expect true actual false

構建一個正則保存爲變量 reg, 連續對同一個字符串字面量 "123" 調用 test 方法,第二次和第四次的結果都是不符合預期的 false,令人震驚

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