首页 > 解决方案 > Javascript 从文本中删除 bb_quotes ([quote..] [/quote])

问题描述

目标是使用 Javascript (vanilla) 删除介于 [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.

标签: javascriptregex

解决方案


您可以使用/\[(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,''));


推荐阅读