runtimeZero runtimeZero - 3 months ago 7
Javascript Question

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

Answer

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