JS Regular string of all IMG image tags and full address and ALT description

Source: Internet
Author: User
Keywords Web page production Ajax javascript
Tags address ajax http image javascript jpg picture description replace

JS Regular string of all IMG image tags and full address and ALT description

<script>
var s= ' <img src= "/upload/image.jpg"/> This is some text <img src= "/upload/image.jpg" alt= "picture description"/> is also some text <img src= " Http://www.jzread.com/upload/image.jpg "/> Last Documents";

var regexp=/(<imgs*src= ") (?: Http://[w.] +)? ([^"]+) ("[^>]+>")/gim

S=s.replace (regexp, ' $1[url]http://www.jzread.com[/url]$2$3 ');

document.write (' <xmp> ' +s+ ' </xmp> ');

</script>

Method Two

<script>
var s= ' <img src= "/upload/image1.jpg"/> This is some text <img src= "/upload/image2.jpg" alt= "Picture description 2"/> is some text <img src= "Http://www.jzread.com/upload/image3.jpg"/> Last
var Imgreg =/<img.*?/?>/gi;

S=s.replace (Imgreg,function (IMGSRC) {
IMGSRC = Imgsrc.replace (/src=) (?: http://www.jzread.com)? *?)" /I, "src=" http://www.jzread.com$1 "")
if (Imgsrc.indexof ("alt=") ==-1) {
IMGSRC = Imgsrc.replace (//?>/i, "alt=" Picture description "/>");
}
return imgsrc;
});

alert (s);
</script>
Feature examples

The string is <img src= "/upload/image.jpg"/> This is some text <img src= "/upload/image.jpg" alt= "picture description"/> is also some text <img src= " Http://www.jzread.com/upload/image.jpg "/> Last file

is a mixed-text HTML code
Want to use the formal expression to automatically add the picture address full domain name, and then automatically add alt description text, but some are already with the ALT description, some are already have the full picture address
Can I replace it with a regular expression?

Related Article

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.