Search code examples
javascriptjqueryregexbbcodephpbb3

removing BBcode from textarea with Javascript


I'm creating a small javscript for phpBB3 forum, that counts how much character you typed in. But i need to remove the special characters(which i managed to do so.) and one BBcode: quote

my problem lies with the quote...and the fact that I don't know much about regex.

this is what I managed to do so far but I'm stranded:

http://jsfiddle.net/emjkc/

var text = '';
var char = 0;


  text = $('textarea').val();
  text = text.replace(/[&\/\\#,+()$~%.'":*?<>{}!?(\r\n|\n|\r)]/gm, '');
  char = text.length;
  $('div').text(char);
$('textarea').bind('input propertychange', function () {

    text = $(this).val();
    text = text.replace(/[&\/\\#,+()$~%.'":*?<>{}!?\-\–_;(\r\n|\n|\r)]/gm, '');

    char = text.length;
    $('div').text(char);
});

Solution

  • You'd better write a parser for that, however if you want to try with regexes, this should do the trick:

    text = $('textarea').val();
    while (text.match(/\[quote.*\[\/quote\]/i) != null) {
       //remove the least inside the innermost found quote tags 
       text = text.replace(/^(.*)\[quote.*?\[\/quote\](.*)$/gmi, '\$1\$2');
    }
    // now strip anything non-character
    text = text.replace(/[^a-z0-9]/gmi, '');