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++;
}
iStartLengthinitialized to ? Also, what isiStartNumber?whileloop will break as soon as a non-number is encountered (isNaNbecomestrue). Looking at your sample input and output, that's not what you want. You probably want to move that condition into anifblock inside your loop.