The usage of / g / I / m in JavaScript regular expression tag, and examples

Time:2020-9-23

1、 JS regular flag / g, / I, / M description

1, / g (globle) indicates that the expression will be used to find all possible matches in the input string, and all matching characters in the full-text search. The returned results can be multiple. If you don’t add / g, you will only match one at most
2, / I (ignorcase) means that the matching is not case sensitive,
Ignore case. Note that only case is ignored, not width.
3, / M (multiple) means multi line matching. What is multi line matching? It’s a potential match that matches both ends of a newline character. Affects the ^ $sign in the regular
M affects ^, $.
If M is not specified: ^ is only at the beginning of the string and $is only at the end of the string. That is: match the beginning and end of the whole string
If M is specified: ^ is at the beginning of each line of the string and $is at the end of each line of the string. That is: match the beginning and end of each line

2、 Example description

1, / g

<script type="text/javascript"> 
str = "tankZHang (231144)"+ 
"tank ying (155445)"; 
res =  str.match (/ tank /); // no / g added 
Alert (RES); // displays a tank 

res =  str.match (/ tank / g); // added / g 
Alert (RES); // displayed as tank, tank 
</script>

Usage of 2, / I

<script type="text/javascript"> 
str = "tankZHang (231144)"+ "tank ying (155445)"; 
 
res = str.match(/zhang/);  
Alert (RES); // display as null 
 
res =  str.match (/ Zhang / I); // added / I 
Alert (RES); // display as Zhang 
</script>

3, / m

<script type="text/javascript"> 
var p = /$/mg; 
var s = '1\n2\n3\n4\n5\n6'; 
Alert (p.test (s)); // displays true 
alert( RegExp.rightContext.replace (/ \ x0a / g, '\ \ a')); // display 
alert( RegExp.leftContext ); // display as vertical 2345 
alert( RegExp.rightContext ); // the display is 6 
 
var p = /$/g; 
var s = '1\n2\n3\n4\n5\n6'; 
Alert (p.test (s)); // displays true 
alert( RegExp.rightContext.replace (/ \ x0a / g, '\ \ a')); // nothing is displayed 
alert( RegExp.leftContext ); // displayed as vertical 123456 
alert( RegExp.rightContext ); // show nothing 
 
var p = /^/mg; 
var s = '1\n2\n3\n4\n5\n6'; 
Alert (p.test (s)); // displays true 
alert( RegExp.rightContext.replace (/ \ x0a / g, '\ \ a')); // it is displayed as 1 / A2 / a3-a4-a5-a6 
alert( RegExp.leftContext ); // displayed as vertical 12345 
alert( RegExp.rightContext ); // the display is 6 
</script>

//From the above example, we can see that / M affects the segmentation of ^ $

The above three examples, / I, / g, / m, can be used in combination. I don’t think / M is very useful

Supplementary notes:

\N matches a newline character. It is equivalent to a and CJ

rightContext

RegExp.rightContext ($’)

This feature is non-standard, please try not to use it in production environment!

The rightcontext nonstandard property is a static and read-only property of a regular expression and contains the latest matching right substring. Regexp. $’is an alias for this property.

grammar

RegExp.rightContext
RegExp[“$'”]

describe
The rightcontext property is static and is not a property of a regular expression independent object. Instead, you should always use it as RegExp.rightContext Or regexp [“$'”].

The value of the rightcontext property is read-only and is modified when the match is successful.

You can’t use the property accessor (regexp. $’) to use shorthand aliases, because the parser will see it as the beginning of a string and throw a syntax error. Use square brackets to access properties.

Examples
Use rightcontext and $’

var re = /hello/g;
re.test(‘hello world!’);
RegExp.rightContext; // ” world!”
RegExp[“$'”]; // ” world!”

leftContext Nonstandard attributes are the static and read-only properties of regular expressions that contain the latest matching left substring. Regexp. $’is an alias for this property.

grammar

RegExp.leftContext
RegExp[‘$`’]

describe

The leftcontext property is static, not a property of a regular expression independent object. Instead, you should always use it as RegExp.leftContext Or regexp [‘$’ ‘].
The value of the leftcontext property is read-only and is modified when the match is successful.

You can’t use the property accessor (regexp. $’) to use short aliases, because the parser will see it as the beginning of the template string and throw syntax error. Use square brackets to access properties.

Examples
Use leftcontext and$`

var re = /world/g;
re.test(‘hello world!’);
RegExp.leftContext; // “hello “
RegExp[‘$`’]; // “hello “

standard

Non standard. It is not part of any current code.