JS foundation — regular correlation functions match, exec, test, replace, search

Time:2020-7-13

match

str.match (string | regexp) // if a string is passed in, it will be converted to a positive expression implicitly through 'new regexp (obj)'
  • If it is not a global match, what is returned isfirstA complete match level whose associatedCapture group information (a group wrapped in parentheses in a regular)
  • If it is a global match, all results of the full regular expression match are returned, and the capture group and related object properties are not returned.

Without capture group, the return is as follows:

Var str \ "Hello, are you {name} {sex} Ben? ";
str.match(/{\[a-zA-Z0-9\u4e00-\u9fa5\]+}/)

JS foundation -- regular correlation functions match, exec, test, replace, search

The case with capture group is returned as follows:

Var STR: "Hello, are you {name} {sex} Ben? ";
str.match(/{([a-zA-Z0-9\u4e00-\u9fa5])+}/)

JS foundation -- regular correlation functions match, exec, test, replace, search

If the result is not matched, it is returnednullOtherwise, the first element of the array is the first fully matched string, followed by the matched values in the capture group, corresponding to the brackets in turn. There are also several object attribute values:indexRepresents the first matching index position,inputRepresents the original string, when the capture group has passed the\<name>The setting name is,groupsReturns the value corresponding to the name.

//Capture group naming
Var STR: "Hello, are you {name} {sex} Ben? ";
str.match(/{(?<var>[a-zA-Z0-9\u4e00-\u9fa5]+)}/)

JS foundation -- regular correlation functions match, exec, test, replace, search

//Matching in global mode
Var STR: "Hello, are you {name} {sex} Ben? ";
str.match (/ {[a-za-z0-9 / u4e00 - \ u9fa5] +} / g); // return ["{name}", "{sex}"]

exec

RegExp.prototype.exec()

  • In non global mode,execPerformance andmatchagreement
  • In global mode,execIt can be executed multiple times, and the results returned by each execution are the samematchFunction. And it will be recorded under regular objectslastIndexPosition, return when not matchednullalsolastIndexIt was recorded as 0;
//Non Global
var reg = new RegExp("{\[a-zA-Z0-9\u4e00-\u9fa5\]+}");
Var STR: "Hello, are you {name} {sex} Ben? ";
reg.exec(str);
//Overall situation
var reg = new RegExp("{\[a-zA-Z0-9\u4e00-\u9fa5\]+}","g");
Var STR: "Hello, are you {name} {sex} Ben? ";
let m = null;
while( (m = reg.exec(str)) != null){
  console.log(m);
  console.log(reg.lastIndex);
}
console.log(reg.lastIndex);

test

RegExpObject.test(string)

It is used to detect whether a string matches a pattern, and the matching returnstrue, otherwise returnfalse

var reg = new RegExp("{\[a-zA-Z0-9\u4e00-\u9fa5\]+}");
Var STR: "Hello, are you {name} {sex} Ben? ";
reg.test (); // returns true, and returns true no matter how many times it is executed

But if the regular expression sets the global flag,test()Execution of changes the lastindex property of the regular expression. Continuous executiontest()Method, subsequent execution will start from the lastindex to match the string.

var reg = new RegExp("{\[a-zA-Z0-9\u4e00-\u9fa5\]+}","g");
Var STR: "Hello, are you {name} {sex} Ben? ";
console.log(reg.test(str) + " " + reg.lastIndex);  //true 11
console.log(reg.test(str) + " " + reg.lastIndex);  //true 16
console.log(reg.test(str) + " " + reg.lastIndex);  //false 0
console.log(reg.test(str) + " " + reg.lastIndex);  //true 11

search

str.search (string | regexp) // if a string is passed in, it will be implicitly converted to a positive expression through 'new regexp (obj)'

If the match is successful, the regular expression is returned in the stringFirst matchThe index of the; otherwise, returns – 1; global mode insensitive.

replace

/*
*When the first parameter is a string, it is not converted to regular, only the first match is replaced.
*The original string will not be changed, and a new string after replacement will be returned
*/
str.replace(regexp|substr, newSubStr|function)

When the second parameter is a string, some special variable names need to be noted:

$$inserts a "$".
$& inserts a matching substring.
$` inserts the content to the left of the currently matched substring.
$'inserts the content to the right of the currently matched substring.
$n the capture group in the regular, $1 represents the first capture group
var reg = new RegExp("{\[a-zA-Z0-9\u4e00-\u9fa5\]+}","g");
Var STR: "Hello, are you {name} {sex} Ben? ";
console.log ( str.replace (reg, "[$&]") // Hello, are you the [{name}]] [{sex}] copy?

When the second parameter is a function, if the first parameter is the regularity of the global pattern, the function will be called many times, and each match will be called. The parameters of the function are variable.

The first parameter is match, which represents the substring of the match. (corresponding to $& above.)
The following P1... PN represents the matching substring in the capture group. (corresponding to $1... $2)
This is followed by offset, which represents the position of the matching string in the string.
Then string, representing the element string;
If there is a named capture group, the last parameter is the object that the named capture group matches
var reg = new RegExp("{\[a-zA-Z0-9\u4e00-\u9fa5\]+}","g");
Var STR: "Hello, are you {name} {sex} Ben? ";
console.log ( str.replace (reg, function (match, offset, STR) {// there is no capture group for the regular
    console.log(match + " " + offset + " "+str);
    return '[' + match + ']';
}))

Related documents

String.prototype.match()
String.prototype.replace()

Recommended Today

Java security framework

The article is mainly divided into three parts1. The architecture and core components of spring security are as follows: (1) authentication; (2) authority interception; (3) database management; (4) authority caching; (5) custom decision making; and;2. To build and use the environment, the current popular spring boot is used to build the environment, and the actual […]