2010-10-30 76 views
1

我需要刪除之前的所有文本,並在此變量中包括「(」和所有文本之後包括「)」「。需要去掉不需要的文本

var this_href = $(this).attr('href'); 

以上代碼生成此...

javascript:change_option('SELECT___100E___7',21); 

然而,它可產生在括號內的任何長度的任何文本。

正則表達式解決方案是可以的。

所以在這種情況下,我想最終與此。

'SELECT___100E___7',21 

回答

2

而不是剝離出來,你不想讓你可以搭配什麼都想你想保留:

/\((.*?)\)/ 

說明:

 
\( Match an opening parenthesis. 
( Start capturing group. 
.*? Match anything (non-greedy so that it finds the shortest match). 
)  End capturing group. 
\) Match a closing parenthesis. 

使用這樣的:

var result = s.match(/\((.*?)\)/)[1]; 
+0

好的,但如何把它放入可用的東西?如何取出舊的變量並創建一個新的變量或用變化的值替換變量中的值?不知道該怎麼做。 – user357034 2010-10-30 07:19:25

+0

謝謝你的例子 – user357034 2010-10-30 13:09:55