Link to home
Start Free TrialLog in
Avatar of Meinhoonaa
Meinhoonaa

asked on

how to extract numbers from string using Javascript?

how to extract numbers from string using Javascript?
Test cases below:
string s = 1AA11111
string s1= 2111CA2

string s result  = 111111
string s1 result = 211112

My code below is not working:
var j = 0;
var startPlateNums = "";
while (j <= iStartLength && !isNaN(iStartNumber.substring(j, j + 1)))
 {
          startPlateNums = startPlateNums + iStartNumber.substring(j, j + 1);
           j++;
 }
ASKER CERTIFIED SOLUTION
Avatar of Dontmilkthis
Dontmilkthis
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,
or this simple regular expression:
function ExtractNumbers(stringToBeProcessed) {
    return stringToBeProcessed.replace(/\D/g, '');

}

Open in new window


Sample:
http://jsfiddle.net/EE_RainerJ/3FyXJ/

HTH
Rainer