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 lot of search, not what I want, and finally leave 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)
<script type= "Text/javascript" >
/ideas are divided into two steps: the author (yanue).
1, match the image of the IMG tag (that is, match all the 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 < IMG src=\ "http:yourweb.com/test.jpg\" Width= ' > 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>
PS: Here again for you to provide 2 very convenient regular expression tools for your reference to use:
JavaScript Regular expression online test tool:
Http://tools.jb51.net/regex/javascript
Regular expression online generation tool:
Http://tools.jb51.net/regex/create_reg
I hope this article will help you with your JavaScript programming.