Usage of/g/i/m in JavaScript regular expressions

Source: Internet
Author: User


Regular thinking is the same, but the specific wording will be different, the/g,/i,/m mentioned here in other places may not be used.

One, JS regular sign/g,/i,/m description

1,/g indicates that the expression will be used to find all possible matches in the input string, and the result can be multiple. If you do not add/g, you will only match one
2,/i indicates matching is case-insensitive
3,/m represents multiple lines matching, what is multiple line matching? is a potential match that matches the ends of the line break. Influence of ^$ symbols in regular

Second, the example illustrates

The use of 1,/g


<script type= "Text/javascript" >
str = "Tankzhang (231144)" +
"Tank Ying (155445)";
res = Str.match (/tank/); No plus/g.
Alert (res); Show a tank

res = Str.match (/tank/g); Added/g.
Alert (res); Show as Tank,tank
<strong></script></strong>

The use 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); Plus/I
Alert (res); Show as Zhang
</script>

The use of 3,/m


<script type= "Text/javascript" >
var p =/$/mg;
var s = ' 1\n2\n3\n4\n5\n6 ';
Alert (P.test (s)); Display as True
Alert (RegExp.rightContext.replace (/\x0a/g, ' \\a ')); Show \a2\a3\a4\a5\a6
alert (Regexp.leftcontext); 2345 shown as Vertical
alert (Regexp.rightcontext); Displayed as 6

var p =/$/g;
var s = ' 1\n2\n3\n4\n5\n6 ';
Alert (P.test (s)); Display as True
Alert (RegExp.rightContext.replace (/\x0a/g, ' \\a ')); Don't show anything.
alert (Regexp.leftcontext); 123456 shown as Vertical
alert (Regexp.rightcontext); Don't show anything.

var p =/^/mg;
var s = ' 1\n2\n3\n4\n5\n6 ';
Alert (P.test (s)); Display as True
Alert (RegExp.rightContext.replace (/\x0a/g, ' \\a ')); Show as 1\a2\a3\a4\a5\a6
alert (Regexp.leftcontext); 12345 shown as Vertical
alert (Regexp.rightcontext); Displayed as 6
</script>

From the example above, we can see that the ^$ segmentation method affected by/M
The three examples mentioned above,/i,/g,/m separately, can be used to arrange combinations. Personally, I don't think it's much use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.