Solved

how to extract numbers from string using Javascript?

Posted on 2013-11-14
2
358 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

777 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