Home > Web Front-end > JS Tutorial > body text

Detailed explanation of the use of regular expression m modifier (multi-line matching)

php中世界最好的语言
Release: 2018-03-30 13:38:46
Original
5632 people have browsed it

This time I will bring you Regular expressionm modifier (multi-line matching) detailed explanation, use of regular expression m modifier (multi-line matching)Notes What 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, a string has 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 browser This metacharacter is supported.
Firefox supports 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));
Copy after login

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));
Copy after login

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);
Copy after login

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);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template