سؤال

I am trying to replace some text. It will make sense if you continue reading.

[quote]Here is a message[/quote]
Message one

[quote]Another message[/quote]
Message two

The above is what is inside my textarea. I want to replace everyting except what is inside the quote-tags. I want to replace them like [b]Message one[/b] so it will add [b] and [/b] around the original message. The end result should look like this:

[quote]Here is a message[/quote]
[b]Message one[/b]

[quote]Another message[/quote]
[b]Message two[/b]

So basically exclude the [quote] and apply [b] and [/b] around the original message. I tried with some exclude regular expression, but I didn't find anything useful.

هل كانت مفيدة؟

المحلول

Try this:

var string = "Replace this[quote]Here is a message[/quote]\nReplace this\n\n[quote]Another message[/quote]\nReplace this as well";
string.replace(/(\[\/quote\]\s*|^)([^[]+?)(\s*\[quote|$)/g, "$1[b]$2[/b]$3");

///// And we get:
// [b]Replace this[/b][quote]Here is a message[/quote]
// [b]Replace this[/b]
//
// [quote]Another message[/quote]
// [b]Replace this as well[/b]

See the jsFiddle as well.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top