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=''/><'/>'/>'/> src='<'/>'/>'/> src='a<'/>'/>'/> src='es'/>de<'/>'/>'/> src='os'/><'/>'/>'/> src='aller<'/>'/>'/> src='es'/>abstract'/><'/>'/>'/> src='['/><'/>'/>'/> src='<'/>'/>'/> src='<'/>'/>'/> src=']

for this image as an example:

Source: regex

Leave a Reply