2017-06-15 196 views
-1

此javascript正則表達式需要從「這是(131PS)現在」中提取「131PS」。
然後,我需要將「131」作爲數字並將「PS」作爲字符串。
有什麼建議嗎? thx括號與正則表達式之間的字符串

myString.match(/\(([^\)]+)\)/ig)[0] 

返回(131PS)這不是預期的結果。

+1

'ARR = myString.match(/ \((\ d + )([az] +)\)/ i)'並使用'arr [1]'和'arr [2]' – anubhava

+0

使用'[1]'獲取第一個捕獲組的值。 – Tushar

回答

1

您需要使用正則表達式捕獲組()工作,分別收回數和字符串,看看:

let rawStr = "this is (131PS) for now"; 
 

 
let theMatch = rawStr.match(/\((\d+)([A-Z]+)\)/); 
 

 
if (theMatch) { 
 
    let theNum = parseInt(theMatch[1]); 
 
    let theString = theMatch[2]; 
 
    
 
    console.log(theNum, theString); 
 
}

+0

@Tushar謝謝你提到這個例子中我不需要這個例子。請再看看。 –

相關問題