2017-06-15 160 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谢谢你提到这个例子中我不需要这个例子。请再看看。 –

相关问题