The example in this article describes the usage of exec in js regular expressions. Share it with everyone for your reference. The details are as follows:
Pay attention to the following points with exec:
1. exec returns an array
2. The attributes of this array include input (the entire string being matched) index (the starting position of the first element matched)
3. lastIndex also has a position pointed to after matching the first attribute. This attribute is intelligently accessed by the RegExp object!!!
4. You can use this attribute to get the string of matched characters
The code is as follows:
function o_exec(){ var str="hjjh,catfff,dog,catarigy,catdog,hjfkhj"; var reg=/cat\S*?\b/g; arr=reg.exec(str); while(reg.lastIndex!=str.length){ alert(arr[0]); arr=reg.exec(str); }
I hope this article will be helpful to everyone’s JavaScript programming design.