简体   繁体   中英

Javascript - Add img tag for all image links on a page

I have some image links in my document like this:

<p> Chrome logo: https://www.google.com/images/icons/product/chrome-48.png</p>
<p> Google logo: https://www.google.com/images/srpr/logo11w.png</p>

I trying to to add img tag to above link to show image instead of those plain links.

Also i have a regex to find all image links but i cant use it with javascript and need your help.

the result must be look like this:

<p> Chrome logo: <img src="https://www.google.com/images/icons/product/chrome-48.png" /></p>
<p> Google logo: <img src="https://www.google.com/images/srpr/logo11w.png" /></p>

This seems trivial… http://jsfiddle.net/La7XE/

document.body.innerHTML = document.body.innerHTML.replace(/(http(s*):\/\/[\w\\x80-\\xff\#$%&~\/.\-;:=,?@\[\]+]*).(gif|png|jpg|jpeg)/gi, '<img src="$1.$3" />');

(MDN)

But you should really do it server-side.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM