Single-line mode and multi-line pattern instance analysis in JS regular expressions _ Regular expressions

Source: Internet
Author: User

This paper analyzes the Single-line mode and multiline pattern in JS regular expression. Share to everyone for your reference. Specifically as follows:

JS Regular expressions are not supported in Single-line mode. That is, you cannot take a piece of content (there is a newline), using a pattern modifier to deal with the whole piece of content as a line to deal with.
For regular multiline substitution, you need to add the/mg pattern enhancer.

Copy Code code as follows:
<script type= "Text/javascript" >
/ /Regular replacement link, Link has newline
Function T1 () {
 var con = document.getelementsbyname (' content ') [0].value;//content <a "" >.....</a>, but there are newline
 var reg =/<a[\s]+[\d\d]*<\/a>/g;//use [\d\d] or [\w\w] or [\s\s] To resolve a problem that cannot be wrapped
 alert (Con.replace (Reg, '----'));
}
//The ending number of each row is replaced with the # number---Multiline mode, add/m (end of each line) and/g (global match) pattern enhancer
Function t2 () {
 var con = document.gete Lementsbyname (' content ') [0].value;//writes several lines of text, each line ends with a number
 var reg =/\d+$/gm;
 alert (Con.replace (Reg, ' # '));
}
</script>
<body>
<textarea rows= "5" cols= "a" name= "content" &G t;</textarea><br/>
<button onclick= "T1 ();" > Regular replacement links (to be considered for line wrapping) </button><br/>
<button onclick= "T2 ();" > Regular multiple-line replacement </button><br/>
</body>

I hope this article is helpful to the regular expression learning.

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.