简体   繁体   中英

Transform string into image with jQuery

Another jQuery question for you guys.

Say I have a url of an image in my web page surrounded by square brackets.

[http://www.example.com/picture.jpg]

How could I, with jQuery transform that string like so...

[http://www.example.com/picture.jpg]

into...

<img src="http://www.example.com/picture.jpg" />

?

I'd do something like this

$("some-selector").each(function(){
    $(this).html($(this).html().replace(/\[(http:.*?)\]/gi, function(str, p1){
        return "<img src='"+p1+"' />";
    }));
});

"some-selector" should try to pinpoint where these string occur. If there is nothing like it... just put "body" and see what happens :)

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