0

Markup:

<div class="post">
    [tag]dfghf[/tag][tag]gh[/tag][tag]cxbcvb[/tag][tag]gh[/tag][tag]cxbcvb[/tag][tag]gh[/tag]
</div>

JS I have so far-

for(var j=0;j<post.length;j++){
    var postText = $(post[j]);
    postText.html(postText.html()
                          .replace('[tag]','<span class="tag">')
                          .replace('[/tag]','</span>')
    );
}

Though this seems to only replace the very first tag. How do I get it to replace all the tags?

Linus Caldwell
  • 10,636
  • 12
  • 45
  • 57
EasyBB
  • 5,392
  • 8
  • 42
  • 73

1 Answers1

2

Try

postText.html(postText
              .html()
              .replace(/\[tag\]/g,'<span class="tag">')
              .replace(/\[\/tag\]/g,'</span>')
              );

Ex:

$('.post').html(function(){
    return $(this)
    .html()
    .replace(/\[tag\]/g,'<span class="tag">')
    .replace(/\[\/tag\]/g,'</span>')
})

Demo: Fiddle

Arun P Johny
  • 376,738
  • 64
  • 519
  • 520