简体   繁体   中英

Extracting digits from a regular expression

var myString = 'Sent from your Zoom trial account - Your verification code is: 621226';
var regEx = /(\d+)$/g;
var result = regEx.exec(myString);

However result = [621226, 621226] instead of just [621226]

http://plnkr.co/edit/8ZbFHDAk0iLwEjpfkQaq?p=info

Use .match() with regEx as parameter chained to myString .

 var myString = 'Sent from your Zoom trial account - Your verification code is: 621226'; var regEx = /(\\d+)$/g; var result = myString.match(regEx); console.log(result); 

plnkr http://plnkr.co/edit/cdwFsPKq9X3tCnub6FzL?p=preview

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM