JS regular match out all pictures and pictures address src method _javascript tips

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.