There is no need to strip the newlines: to match any character including newlines use [\s\S]
instead of .
.
The multiline modifier m
, which makes the anchor tags ^
and $
match the beginning and end of a line instead of the whole string, is unnecessary also.
Here is a solution that also avoids the repeated match
calls:
var t;
while ( t != text ) {
t = text;
//text = text.replace( /\[quote(?:(?!\[quote)[\s\S])+?\[\/quote\]/g, '' );
text = text.replace( /^([\s\S]*)\[quote[\s\S]+?\[\/quote\]/g, '$1');
}
The commented-out line is an alternative version which should work equally well.
Instead of a greedy match it uses a negative look-ahead to ensure that only the deepest quote tags are matched. It is enclosed in parentheses with the [\s\S]
so that it looks ahead before every character between the quote tags is matched, and prevents a match if [quote
appears.
It is hard to say which would be more efficient.
See JSFIDDLE.