Replace the image address img tag with a regular expression

Source: Internet
Author: User

The solution is as follows:

Copy codeThe Code is as follows:
Content. replace (/] * src = ['"] ([^'"] +) [^>] *>/gi, function (match ){
Console. log (match );
});

The output result is:

Copy codeThe Code is as follows:

The entire img tag is obtained, but what I expect is the URL in src, so you only need to return the new address in function (match.
So, it's stuck here...
Later, Google searched for the keyword "javascript replace callback" and found "replace callback function with matches" in stackoverflow to know that function (match) has other parameters.

Then, change the code below to solve the problem.

Copy codeThe Code is as follows:
Content. replace (/] * src = ['"] ([^'"] +) [^>] *>/gi, function (match, capture ){
Console. log (capture );
});

Output result:

Copy codeThe Code is as follows:
Http://www.bkjia.com/uploads/allimg/131230/0300101S7-0.gif

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.