views:

232

answers:

1

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" />

?

+1  A: 

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 :)

Victor
Thank you but no joy I'm afriad
Keith Donegan
There was a mistake in it... I have to use $(this). Maybe it works now :)
Victor
So so close, thank you! - I get: <img src="[http://www.example.co.uk/wp-content/uploads/2009/10/virgin_media.jpg"/> - Just need to get rid of the left bracket?
Keith Donegan
Ah! This guy know Regex X( http://stackoverflow.com/questions/1077541/regular-expression-joke-explanation
Rakesh Juyal
Another stupid error :P it is done now, I think!
Victor
Victor, you are an absolute star, thank you 1000 times!
Keith Donegan