从文本中删除bb_quotes([quote ..] [/ quote])的JavaScript

时间:2019-05-30 22:09:40

标签: javascript regex

目标是使用Javascript(原始语言)删除介于[quote] [/ quote]和[quote = something] [/ quote]之间的所有文本(包括首字母)(不区分大小写) )。如果删除引号后还存在双精度空格,则最好也将其删除。我按如下方式尝试了此Javascript,即:

t.replace(/\[quote.*\](.*?)\[\/quote\]/gi,'')

,但是我没有得到正确的结果。正确的方法是什么?

var t='Starting [QUOTE]this should be ignored hello[/quote] it. This is not quote and [quote=frank]HELLO quotes[/quote] Marky Mark 84WD. Last [quote=irene]try.[/quote]';

console.log(t.replace(/\[quote.*\](.*?)\[\/quote\]/gi,''));

//Current result: Starting.
//Expected result: Starting it. This is not quote and Marky Mark 84WD. Last

var t='[Quote]this should be ignored hello[/quote]. This is not quote and [quote=frank]HELLO quote[/quote] Marky Mark 84WD.';

console.log(t.replace(/\[quote.*\](.*?)\[\/quote\]/gi,''));

//Current result: Marky Mark 84WD.
//Expected result: . This is not quote and Marky Mark 84WD.

1 个答案:

答案 0 :(得分:2)

您可以使用/\[(quote)[^\]]*](.*?)\[\/\1\]/gi进行过滤:

var t='Starting [QUOTE]this should be ignored hello[/quote] it. This is not quote and [quote=frank]HELLO quotes[/quote] Marky Mark 84WD. Last [quote=irene]try.[/quote]';

console.log(t.replace(/\[(quote)[^\]]*](.*?)\[\/\1\]/gi,''));

var t='[Quote]this should be ignored hello[/quote]. This is not quote and [quote=frank]HELLO quote[/quote] Marky Mark 84WD.';

console.log(t.replace(/\[(quote)[^\]]*](.*?)\[\/\1\]/gi,''));