Let's look at match () usage first.
Grammar
The code is as follows |
Copy Code |
Stringobject.match (Searchvalue) Stringobject.match (RegExp) |
In this case, we'll use a global matching regular expression to retrieve all the numbers in the string:
The code is as follows |
Copy Code |
<script type= "Text/javascript" > var str= "1 plus 2 equal 3" document.write (Str.match (/d+/g)) </script> Output: 1,2,3 |
Okay, that's a lot of talk now. Match the content between two elements
The code is as follows |
Copy Code |
<script> try{ Str= "<p>abcdefg</p><p>abcdefghijkl</p>"; Re1=str.match (/<p>[ww]+?</p>/ig); Alert ("Non-greedy mode: rnrn1:" +re1[0]+ "RN2:" +re1[1]); Re1=str.match (/<p>[ww]+</p>/ig); Alert ("Greedy mode: Rnrn" +re1); Re1=str.match (/<p> (. +?) </p>/i); Alert ("Non-greedy mode, and do not mark: rnrn1:" +re1[1]); Re1=str.match (/<p> (. +) </p>/i); Alert ("Greedy mode, and do not mark: Rnrn" +re1[1]); }catch (e) {alert (e.description)} </script> |