Hybrid raw/formatted html in preview

by Kehoru

HTML

<textarea name="" id="textareainput" cols="30" rows="10">
[b]bold[/b] normal <strong>strong</strong> <em>em</em> normal [i]italic[/i]


[code]we should see <strong>each</strong> html [u]tag[/u][/code]
</textarea>

<div class="preview"></div>

JavaScript

$('.preview').html( $('#textareainput').val() );

// var codetag = str.substring(
//    str.lastIndexOf("<test>") + 1, 
//     str.lastIndexOf("</test>")
// );

var bbcodeh = $('.preview').html().
  replace(/(\[((\/?)(b|u|i|s|sub|sup))\])/gi, '<$2>').
  replace(/\n/g, "<br />").
  replace(/(\[code\])/gi, "<test>").
  replace(/(\[\/code\])/gi, "</test>");

$('.preview').html(bbcodeh);
$('.preview test').each(function() {
$(this).text(this);
});

// $(codetag).text();