Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how to extract numbers from string using Javascript?

Posted on 2013-11-14
2
Medium Priority
?
370 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 2000 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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…

722 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