<script type="text/javascript"> var someText= "web2.0 .net2.0" ; var pattern=/(\w+)(\d)[.](\d)/; var a=pattern.exec(someText); var a1=pattern.exec(someText); var b=someText.match(pattern); var b1=someText.match(pattern); console.log("exec",a) console.log(a1) console.log("match",b) console.log(b1) </script>
Without global g, the result is the same. Only the first string that meets the condition is returned
(exec (4) ["web2.0", "web", "2", "0", index: 0, input: "web2.0 .net2.0", groups: undefined] (4) ["web2.0", "web", "2", "0", index: 0, input: "web2.0 .net2.0", groups: undefined] match (4) ["web2.0", "web", "2", "0", index: 0, input: "web2.0 .net2.0", groups: undefined] (4) ["web2.0", "web", "2", "0", index: 0, input: "web2.0 .net2.0", groups: undefined]
Result analysis: without global results, there is no difference. They all return an array, and the first one in the array is the matching result of the whole regular expression, followed by the matching result of the sub expression
The result of global g is added
<script type="text/javascript"> var someText= "web2.0 .net2.0" ; var pattern=/(\w+)(\d)[.](\d)/g; var a=pattern.exec(someText); var a1=pattern.exec(someText); var b=someText.match(pattern); var b1=someText.match(pattern); console.log("exec",a) console.log(a1) console.log("match",b) console.log(b1) </script>
exec (4) ["web2.0", "web", "2", "0", index: 0, input: "web2.0 .net2.0", groups: undefined] (4) ["net2.0", "net", "2", "0", index: 8, input: "web2.0 .net2.0",groups: undefined] match (2) ["web2.0", "net2.0"] (2) ["web2.0", "net2.0"]
Result analysis: global matching g was added
Match returns the matching result of the entire regular expression. No matter how many times str.match(reg) is executed, the returned result is the same. The result of the sub regular expression is not returned
exec when global is added, it means that the operation of step-by-step execution is enabled. Once executed, it returns the matching to the detailed data. There are sub regular expressions, and the returned results can be executed all the time exec (STR) will finally return null, that is, after the global matching is completed, return null and execute reg exec (STR) will start from scratch and repeat the previous round of steps
Look at the code below
<script type="text/javascript"> var str = "abc1.net2" var reg = /\w+(\d)/g var a = reg.exec(str); var b = reg.exec(str); var c = reg.exec(str) var d = reg.exec(str) var e = reg.exec(str) var f = reg.exec(str) console.log(a); console.log(b) console.log(c) console.log(d) console.log(e) console.log(f) </script>
result
(2) ["abc1", "1", index: 0, input: "abc1.net2", groups: undefined] (2) ["net2", "2", index: 5, input: "abc1.net2", groups: undefined] null (2) ["abc1", "1", index: 0, input: "abc1.net2", groups: undefined] (2) ["net2", "2", index: 5, input: "abc1.net2", groups: undefined] null
Summary; Match is a string method str.match(reg) when reg = /... / G has global g, it will not return the result of sub regular expression matching, but only the result of one-time matching of the whole expression
There is no g matching result for a subregular expression
exec is a regular expression method reg exec (STR) when reg has a global, the global search step is enabled. Reg. Is not executed once exec (STR) returns the result that the entire regular expression matches the sub regular expression
In addition, the order of the regular lookup string
When there is no global lookup g
Search the string from left to right to match the beginning of the regular expression, and then match the end of the regular expression from the right of the string. When the end matches, intercept this character and return
When there is g
Search the string from left to right to match the beginning of the regular expression, and then match the end of the regular expression from the right of the string. When the end matches, intercept this character and return, and then record the end character and the position lastindex in the whole string
Then start from the position of the whole string lastindex, match the beginning of the whole regular expression from left to right, and then match the end of the regular expression from the right of the string and return such a match