I have the following regular expression in javascript :
var the_regexp = /^\/([!\/]*)\/?(\w*)\??([\=|\w]*)\/?$/gi
It Firefox and Chrome console, it find a match for the string “/d” once every two attemps.
>the_regexp
/^\/([!\/]*)\/?(\w*)\??([\=|\w]*)\/?$/gi
>the_regexp.exec("/d")
null
>the_regexp.exec("/d")
["/d", "", "d", ""]
>the_regexp.exec("/d")
null
>the_regexp.exec("/d")
["/d", "", "d", ""]
Can somebody explain this behaviour ?
MDN Docs:
So when your regex has a g flag, and you use the exec method once, the next time you execute it it will search for a match after the first match. In this case, there is none: exec will return null and the lastIndex property will be reset.
For example:
I’m sorry for my bad wordings etc, I’m not fully awake…