2012-03-15 52 views
0

因此,目前我正在試圖添加一個功能,我已經作出的腳本。greasemonkey userscript注入變量在另一個變量

目前userscript會將一些文本變爲變量。現在也有2個變量,目前用於將文本添加到第一個變量中的文本和結尾。下面是一個代碼示例,使您更好地瞭解目前正在做

elmTextarea.value = opening + elmTextarea.value + closing; 

什麼在哪裏elmTextarea是,userscript了開放和關閉是我把在一開始的東西,它結束的字符串。現在

,我希望發生的是,如果變量elmTextarea包括任何[報價*] blahblahblah了[/ quote]它基本上將排除這些領域(不是明星可以是任何東西,它是在格式

[quote='name' pid='20784507' dateline='1331755619']) 

但也可能是剛剛[報價]

因此,這裏是一個簡單的例子,所以你可以更好的瞭解

如果elmTextarea是

blahbla 
[quote='name' pid='20784507' dateline='1331755619'] 
some more text 
[/quote] 

here is some more 

[quote='name' pid='20523454507' dateline='1335435619'] 
some more text in here 
[/quote] 

and finally this text 

它會成爲

opening + blahbla + closing 
[quote='name' pid='20784507' dateline='1331755619'] 
some more text 
[/quote] 

opening + here is some more + closing 

[quote='name' pid='20523454507' dateline='1335435619'] 
some more text in here 
[/quote] 

opening + and finally this text + closing 

所以,我有一個想法,這是如何工作的,這裏是我試圖實現

var openingquote = closing + "[quote"; 
var closingquote = "[/quote]" + opening; 
elmTextarea.value = opening + elmTextarea.value + closing; 
elmTextarea.value = elmTextarea.value.replace(/[quote/gim, openingquote); 
elmTextarea.value = elmTextarea.value.replace(/[\/quote]/gim, closingquote); 

但這些行添加到我的代碼,使我的整個腳本不行。任何想法,爲什麼這不起作用,以及如何解決它。

回答

1

方括號在正則表達式中有特殊含義。這些也應該逃脫:

elmTextarea.value = elmTextarea.value.replace(/\[quote/gim, openingquote); 
elmTextarea.value = elmTextarea.value.replace(/\[\/quote]/gim, closingquote); 
//           ^Escape [ using \[