views:

125

answers:

1

Hi all!

I'm working on a php dynamic web page that has a element that let user write and their text is show in "real time" just javascript-processing the text & tags on a element.

I change the "b","u", and tags between brackets (like phpbb style) to its html equivalent like "strong" ,"u", and so using javascript regexps.

Problem occurs when i need to process the URL tag, when taking URL from [URL=http://.....

How can i replace it to 'a href="http:/..." ' thing?

Thanks in advance

A: 

This should work:

str.replace(/\[url=([^\s"<>\]]+)\]/gi, '<a href="$1">$1</a>');

That should take the parameters in [url=...] and, barring any funny business (<, > or spaces), change it to a hyperlink, using the URL as both the destination and the link text.

This will allow things like [url=javascript:while(1)alert('Boo!')], which will produce a link that, when clicked, will really annoy the user - you'll have to add some sanitisation filtering to block stuff like that.

Samir Talwar
Thanks! It worked perfectly :)Just what I needed :D
Ragnagard
Great. I've just noticed I didn't add the double-quote character ( " ) to the list of disallowed characters - I'd recommend squeezing it in so the tag link doesn't end early and screw things up. I've added it in to the regex now.
Samir Talwar