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.