- Article 1
- Article 2
- Article 3
- Article 4
- Article 5
- Article 6
This is what I wrote last year (2009.11.02). At that time, I felt that I was constrained by regular expressions everywhere and decided to read all the posts related to regular expressions without worry. Therefore, this series of content is all out of worry-free experts.
Problem: Check that a string contains only letters or numbers.
<Br/> var STR = "www" <br/> var Re =/^ [A-Za-z0-9] * $/<br/> alert (Re. test (STR) <br/>
RunCode
^ Matches the start position of the input string. Unless used in the square brackets expression, this character set is not accepted. To match the ^ character itself, use \ ^.
$ Matches the end position of the input string. If the multiline attribute of the Regexp object is set, $ also matches '\ n' or' \ R '. To match the $ character, use \ $.
A piece of HTML code, such:
<A href = "www.51js.com" target = "_ Self"> abcdefg </a>
How to replace only content outside the hyperlink tag with <font color = "red"> situ zhengmei </font>
Instead of replacing the content in the hyperlink label? What should I do with regular expressions? The result is <a href = "www.51js.com" target = "_ Self"> <font color = "red"> situ zhengmei </font> bcdefg </a>.
<Br/> var STR = 'abcdefg'; <br/> // $1 $2 indicates the start label of the element, and $3 indicates the innertext before the content to be searched, $4 is innertext behind the content to be searched, and $5 is the end tag of the element <br/> alert (Str. replace (/)(. *) (. *) ()/g, "a $4 $5"); <br/>
Run code
<Br/> function example (STR, search) {<br/> var Re =/(. + ?) /G; <br/> Str. match (re); <br/> var str2 = Regexp. $2, // obtain anchor's innerhtml first; <br/> re2 = new Regexp ("(" + SEARCH + ")", "G "), // A Good Idea. Use the content to be modified for segmentation <br/> newstrarr = Str. split (str2); // then use the modified content to bond the split array element jion! <Br/> return newstrarr. join (str2.replace (re2, "$1"); <br/>}</P> <p> var STR = 'abcadefg', searchstr = ""; <br/> alert (example (STR, searchstr); <br/>
Run code
Available/) (. *) ()/GExtract HTML tags with the content in the middle of $3. Or/(. + ?) /GThe content in the middle is $2.
Problem: Replace "/" in a string "\"
<Br/> alert ("A/B". Replace (// ig, "\"); <br/>
Run code