Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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, '');

Solution 2:

I'm not sure if this would work, but I think you can replace all bbcodes with a regex like this:

var withoutBBCodes = message.replace(/\[[^\]]*\]/g,"");

It just replaces everything like [any char != ']' goes here]

EDIT: sorry, didn't see that you only want to replace [quote] and not all bbcodes:

var withoutBBQuote = message.replace(/\[[\/]*quote[^\]]*\]/g,"");

EDIT: ok, you also want quoted content removed:

while (message.indexOf("[quote") != -1) {
    message = message.replace(/\[quote[^\]]*\]((?!\[[[\/]*quote).)*\[\/quote\]/g,"");
}

I know you already got a solution thanks to @guido but didn't want to leave this answer wrong.


Post a Comment for "Removing BBcode From Textarea With Javascript"