Javascript global string replace

I have a comment textarea #reply-textarea:

var message = $('#reply-textarea').val();
var message = message.replace(/[img]/g,"<img src='");
var message = message.replace(/[/img]/g,"'/>");

This allows an image, for example [img]http://link-to-image.jpg[/img] to be displayed as <img src='http://link-to-image.jpg'/> onto the page.

However it’s producing something weird like:

[<'/>'/>'/> src='<'/>'/>'/> src='<'/>'/>'/> src=']http:'/>'/>www.joo<'/>'/>'/> src='laworks.net'/><'/>'/>'/> src='<'/>'/>'/> src='a<'/>'/>'/> src='es'/>de<'/>'/>'/> src='os'/><'/>'/>'/> src='aller<'/>'/>'/> src='es'/>abstract'/>7.jp<'/>'/>'/> src='['/><'/>'/>'/> src='<'/>'/>'/> src='<'/>'/>'/> src=']

for this image as an example:

http://www.joomlaworks.net/images/demos/galleries/abstract/7.jpg


Source: regex

Leave a Reply