使用Javascript从textarea中删除BBcode [英] removing BBcode from textarea with Javascript

查看:162
本文介绍了使用Javascript从textarea中删除BBcode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为phpBB3论坛创建一个小型javscript,它会计算您输入的字符数. 但是我需要删除特殊字符(我设法这样做)和一个BBcode:quote

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);
});

推荐答案

您最好为此编写一个解析器,但是,如果要尝试使用正则表达式,请

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

这篇关于使用Javascript从textarea中删除BBcode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆