The example of this article tells JS is matching all pictures and image address Src method. Share to everyone for your reference. The specific analysis is as follows:
There are a lot of times we need to use the picture in the article, and mainly use its image address, this time we need to go through the regular match out of the picture tag, and then do the data we need
Usually also not how to use regular, a do not learn to forget, the recent project needs, and then went to Goole, good chaos! A large pile, nor is it what I want, and finally left myself one has been used after:
Implementation: Through JS regular match out all pictures and all pictures address src.
Train of thought: 1. Match the image of the IMG tag (that is, match all the pictures), filter other unwanted characters
Loops from Matching Results (IMG tags) to the image address (that is, the SRC attribute)
Code: (You can copy it to the local test)
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The |
<script type= "Text/javascript" >/ideas are divided into two steps: Author (yanue).//1, matching the image of the img Check (ie match all pictures), filter other unwanted characters//2. Loops from Matching results (img tags) to match the image address (that is, the SRC attribute) var str = "This is test string 123 and the end 33! |/>)/gi; Matching src attribute var srcreg =/src=[' "]? ([^'"]*) [' "]?/i; var arr = Str.match (Imgreg); Alert (' array of all successfully matched pictures: ' +arr '); for (var i = 0; i < arr.length i++) {var src = arr[i].match (srcreg);//Get Picture address if (Src[1]) {alert (' matched picture address ' + (i+1) + ': ' + SRC[1]); Of course you can also replace the SRC attribute if (src[0]) {var t = src[0].replace (/src/i, "href");//alert (t);} </script> |
The
wants this article to help you with your JavaScript programming.