Solved

how to extract numbers from string using Javascript?

Posted on 2013-11-14
2
364 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

729 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