2014-03-30 25 views
1

我想替換一些文本。如果你繼續閱讀,這將是有意義的。jQuery - 替換MyBB消息,但不是它的一部分

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

[quote]Another message[/quote] 
Message two 

以上就是我textarea的內。我想替換除quote-tags內部的所有內容。我想將它們替換爲[b]Message one[/b],因此它會在原始消息周圍添加[b][/ b]。最終的結果應該是這樣的:

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

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

所以基本上排除原始消息的[quote]和apply [b]和[/ b]。我嘗試了一些排除正則表達式,但我沒有找到任何有用的東西。

+0

這兩者之間總會有新的界限嗎? – adeneo

+0

或可能:。!'$( 「#userinput」)VAL()分(/ \ n /)地圖(函數(V){ 回報v &&〜v.indexOf( '[報價]')「? [b]'+ v +'[/ b]':v; })。join('\ n');'http://jsfiddle.net/sG7DY/ –

回答

2

試試這個:

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] 

jsFiddle爲好。

+0

偉大的工作。它幾乎完美地工作。是否有可能在最後刪除新行?請看:http://jsfiddle.net/p8jzV/1/ – MortenMoulder

+0

改變'$( 「文本區域」)VAL()''到$ .trim($( 「文本區域」)VAL()。)'。 [我更新了你的小提琴。](http://jsfiddle.net/p8jzV/4/) –

+0

完美。現在另一個問題(我猜)。如果你做了不止一行,它也會爲這些添加[b]和[/ b]。難道不可能在線上添加b標籤而不是將它們應用到每條線上? – MortenMoulder