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,908
  • 12
  • 46
  • 58
EasyBB
  • 6,176
  • 9
  • 47
  • 77

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
  • 384,651
  • 66
  • 527
  • 531