2011-08-31 147 views
1

我正面臨着這個問題。我得到這樣的字符串。需要幫助查找javascript代碼

'=--satya','=---satya1','=-----satya2'. 

現在我的問題是,我不得不刪除這些特殊字符,打印字符串這樣

'satya' 
'satya1' 
'satya2' 

請幫忙解決這個問題?

回答

0

使用JavaScript函數replace它可以幫助你用正則表達式這種情況下

var string = '=---satya1'; 
string = string.replace(/[^a-zA-Z0-9]/g, ''); 
+0

這樣的事情: str.replace(/(= | - )* /,「」) –

1

您可以提取使用正則表達式,如

/\'\=-{0,}(satya[0-9]{0,})\'/ 

活生生的例子信息:http://jsfiddle.net/LFZje/

正則表達式匹配

字面'
字面=
零個或多個-
啓動一個捕獲團的捕捉
- 字面satya
- 零個或多個numbers
完使用代碼捕獲組
字面'

然後如

var regex = /\'\=-{0,}(satya[0-9]{0,})\'/g; 
while((match = regex.exec("'=--satya','=---satya1','=-----satya2'")) !== null) 
{ 
    // here match[0] is the entire capture 
    // and match[1] is tthe content of the capture group, ie "satya1" or "satya2" 
} 

查看實時示例的更多詳細信息。

3

使用String.replace

var s = '=---satya1'; 
s.replace(/[^a-zA-Z0-9]/g, ''); 

更換所有非字母和非數字字符或

s.replace(/[-=]/g, ''); 

刪除所有-=人物,甚至

'=---satya-1=test'.replace(/(=\-+)/g, ''); // out: "satya-1=test" 

到防止進一步刪除-=