JavaScript Regular expression decoration appended multiline (/m) usage

Source: Internet
Author: User

JavaScript regular expressions have 3 modifiers of/I,/m, and/g. /I is the most common, and best understood, case-insensitive representation of regular expression matches.

var regex =/abc/i;alert (Regex.test ("abc"));//true


/ m stands for multiline mode multiline, if the target string does not contain a newline character \ n, which is only one row, then the/M modifier has no meaning.

var multiline =/abc/m;var Singleline =/abc/;//destination string does not contain newline characters \nvar target = "abcabcabc";

The/ m modifier does not make any sense if the regular expression does not contain the beginning or end of a ^ or $ match string.

The regular expression does not contain ^ or $var multiline =/abc/m;var Singleline =/abc/;var target = "ABCAB\NCABC";

This means that the/ m modifier is only useful if the target string contains \ n and the regular expression contains ^ or $. If Multiline is false, "^" matches the starting position of the string, and "$" matches the end position of the string. If Multiline is true, then "^" matches the position of the beginning of the string and the location after "\ n" or "\ R", and "$" matches the position of the end of the string and the location before "\ n" or "\ r".

var mutiline =/^abc/m;var Singleline =/^abc/;var target = "EF\R\NABCD"; alert (Mutiline.test (target));//truealert ( Singleline.test (target));//false


\ r \ nyou represent line breaks under Windows, if only 1 \ n is the same effect. Because target is not a string that begins with ABC, the result of matching singleline is false, because target is a multiline string (containing \ n), and the 2nd line starts with ABC, so the match multiline result is true.


JavaScript Regular expression decoration appended multiline (/m) usage

Related Article

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.