Solved

how to extract numbers from string using Javascript?

Posted on 2013-11-14
2
357 Views
Last Modified: 2013-11-14
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++;
 }
0
Comment
Question by:Meinhoonaa
2 Comments
 
LVL 5

Accepted Solution

by:
Dontmilkthis earned 500 total points
ID: 39649608
So here's a quick function I made that extracts the numbers.

main difference is the while loop you had, was breaking out as soon as it hit a number, ie the first character in your test strings. so it was never executing the code in the loop.




function getNumbers(string)
{
      var iStartLength = string.length;
      var startPlateNums = "";
      var j = 0;
      var startPlateNums = "";
      while (j <= iStartLength)
       {
             if(!isNaN(string.charAt(j)))
             {
                startPlateNums = startPlateNums + string.charAt(j);
          }
                 j++;
       }
       return startPlateNums;
}


or a bit cleaner with a for loop

function getNumbers(string)
{
      var startPlateNums = "";
      for (var j=0; j <= string.length; j++)
       {
             if(!isNaN(string.charAt(j)))
             {
                startPlateNums = startPlateNums + string.charAt(j);
          }
       }
       return startPlateNums;
}
0
 
LVL 44

Expert Comment

by:Rainer Jeschor
ID: 39649722
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
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now