This time I will bring youRegular expressionm modifier (multi-line matching) detailed explanation, use of regular expression m modifier (multi-line matching)NotesWhat are they? Here are actual cases. Let’s take a look.
Regular expression m modifier:
The m modifier specifies that the regular expression can perform multi-line matching. The function of the
m modifier is to modify the role of ^ and $ in regular expressions so that they represent the beginning and end of the line respectively.
In the default state, astringhas only one starting ^ and ending $ regardless of whether it is newline or not. If multi-line matching is used, then each line has a ^ and ending $.
Syntax structure:
ConstructorMethod:
new RegExp("regexp","m")
Object direct quantity method:
/regexp/m
Browser support:
IE browserThis metacharacter is supported.
Firefoxsupports this metacharacter.
Google Chrome supports this metacharacter.
Example code:
Example 1:
var str="This is an\n antzone good"; var reg=/an$/; console.log(str.match(reg));
The above code cannot match the string "an". Although there is a newline after "an", it does not No multi-line matching is used, so it is not the end of the string line.
Example 2:
var str="This is an\n antzone good"; var reg=/an$/m; console.log(str.match(reg));
The above code can match the string "an" because it uses multi-line matching.
Example 3:
var reg = /^b/; var str = 'test\nbbs'; execReg(reg,str);
The match failed because there is no b character at the beginning of the string. But after adding the m modifier:
Example 4:
var reg = /^b/m; var str = 'test\nbbs'; execReg(reg,str);
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!
Recommended reading:
Detailed explanation of the use of regular expression \W metacharacters (with code)
Use of regular pattern modifiers Detailed explanation
The above is the detailed content of Detailed explanation of the use of regular expression m modifier (multi-line matching). For more information, please follow other related articles on the PHP Chinese website!