Example Analysis of single-row and multi-row modes in js regular expressions, and Example Analysis of Regular Expressions

Source: Internet
Author: User

Example Analysis of single-row and multi-row modes in js regular expressions, and Example Analysis of Regular Expressions

This article analyzes the single-row mode and multi-row mode in the js regular expression. Share it with you for your reference. The details are as follows:

Js regular expressions do not support the single-line mode. That is to say, a certain segment of content (with a line break) cannot be processed using a pattern modifier to treat the entire segment as a line.
To replace multiple regular expressions, add the/mg mode modifier.
Copy codeThe Code is as follows: <Head>
<Script type = "text/javascript">
// Regular Expression replacement link with line feed
Function t1 (){
Var con = document. getElementsByName ('content') [0]. value; // The content contains <a href = ""> ..... </a>, but with line breaks
Var reg =/<a [\ s] + [\ d \ D] * <\/a>/g; // use [\ d \ D], [\ w \ W], or [\ s \ S] to solve the problem of line break failure.
Alert (con. replace (reg ,'----'));
}
// Replace the number at the end of each row with the number # --- multi-row mode, and add the/m (each row is treated as the end) And/g (Global match) mode delimiters.
Function t2 (){
Var con = document. getElementsByName ('content') [0]. value; // write a few lines of text, each line ends with a number
Var reg =/\ d + $/gm;
Alert (con. replace (reg ,'#'));
}
</Script>
</Head>
<Body>
<Textarea rows = "5" cols = "30" name = "content"> </textarea> <br/>
<Button onclick = "t1 ();"> Regular Expression replacement Link (line feed is required) </button> <br/>
<Button onclick = "t2 ();"> replace multiple regular expressions </button> <br/>
</Body>
</Html>

I hope this article will help you learn regular expressions.

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.