SO - tag replacement

http://stackoverflow.com/questions/8378575/get-string-between-two-strings-using-jquery .

by dzejkej

HTML

<div class="comment-body">
  [img]http://topnews.net.nz/images/YouTube-3.jpg[/img]  
  some text here
  [img]http://www.timmckennaphoto.com/media/logo/Youtube-logo.png[/img]
</div>

<div class="comment-body">
  [img]http://blog.brightcove.com/sites/all/uploads/FACEBOOK%20ICON.png[/img] text
</div>

<div class="comment-body">
  nothing to replace here
</div>

CSS

div {
    border: 1px solid black;
    margin-bottom: 10px;
}

JavaScript

// inputText - input text that contains tags
// tagName - name of the tag we want to replace
// tagReplace - replacement for the tag, "$1" will get replaced by content of the tag
function replaceText(inputText, tagName, tagReplace) {
    var regExp = new RegExp('\\[' + tagName+ '\\]([^\\[]*)\\[\/' + tagName + '\\]', 'g');
    return inputText.replace(regExp, tagReplace);
}

$('.comment-body').each(function() {
    var $this = $(this);
    var replacedText = replaceText($this.text(), 'img', '<img src="$1" \/>');
    $this.html(replacedText);
});