/.+\? (.+)$/.exec(url)[1]; That will be? The following string is extracted. I’ve been using split strings to get parameters, so learn a little bit. Record here, a few days forget good review…

The most basic knowledge of regular can refer to:

Juejin. Cn/post / 684490…

www.w3school.com.cn/jsref/jsref…

The case where grouping is not used

Execute the following code:

var str = 'We can! '
var patt = /[A-Za-z]+/g // Interval matching
console.log(patt.exec(str))
console.log(patt.exec(str))
console.log(patt.exec(str))
Copy the code

We get:

["We", index: 0, input: "We can!", groups: undefined]
["can", index: 3, input: "We can!", groups: undefined]
null
Copy the code

We can see that with /g global matching, exec is stateful and returns the matched value in turn, along with the first letter of the matched value corresponding to the subscript. When global matching is not used, it is stateless and returns the same result each time (that is, the first matching string).

Use of groupings

Execute the following code:

var str = 'We can! '
var patt = /([A-Za-z])+/g // Interval matching + grouping
console.log(patt.exec(str))
console.log(patt.exec(str))
console.log(patt.exec(str))
Copy the code

We get:

["We", "e", index: 0, input: "We can!", groups: undefined]
["can", "n", index: 3, input: "We can!", groups: undefined]
null
Copy the code

Note that this returns a member of the array with subscript 1. This is the last result that can be matched for the corresponding group. This result looks strange.

var re = /666\s(brown).+? (jumps)/ig;
var result = re.exec('666 Brown 121212 Jumps 2333');
console.log(result)
Copy the code
["666 Brown 121212 Jumps", "Brown", "Jumps", index: 0, input: "666 Brown 121212 Jumps 2333", groups: undefined]
Copy the code

Result [1] corresponds to the result of the first grouping (Brown) matching.

Refer to the specification

Developer.mozilla.org/zh-CN/docs/…