Solved

how to extract numbers from string using Javascript?

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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.
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…
Suggested Courses

617 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