25

How do you filter only the numbers of a string?

Example Pseudo Code:
number = $("thumb32").filternumbers()
number = 32
OCDev
  • 2,280
  • 3
  • 26
  • 37
Tomkay
  • 5,120
  • 21
  • 60
  • 92
  • Could you give some example data? dkwlj-32-fjkij-ff0-33? – sunn0 Dec 16 '10 at 12:14
  • 2
    Please read the FAQ: http://stackoverflow.com/faq Questions should be *questions*. And the more effort you put into your question, the better the quality and quantity of answers you'll recent (and the more respect for the venue you're showing). – T.J. Crowder Dec 16 '10 at 12:15

2 Answers2

75

You don't need jQuery for this - just plain old JavaScript regex replacement

var number = yourstring.replace(/[^0-9]/g, '')

This will get rid of anything that's not [0-9]

Edit: Here's a small function to extract all the numbers (as actual numbers) from an input string. It's not an exhaustive expression but will be a good start for anyone needing.

function getNumbers(inputString){
    var regex=/\d+\.\d+|\.\d+|\d+/g, 
        results = [],
        n;

    while(n = regex.exec(inputString)) {
        results.push(parseFloat(n[0]));
    }

    return results;
}

var data = "123.45,34 and 57. Maybe add a 45.824 with 0.32 and .56"
console.log(getNumbers(data));
// [123.45, 34, 57, 45.824, 0.32, 0.56];
Jonathon Bolster
  • 15,811
  • 3
  • 43
  • 46
  • 8
    Why is regex so hard? It looks so foreign and my brain just seems to shut down immediately because it gets very overwhelmed by a lot of punctuation... and symbols, if I don't know their meaning but know they mean something... Why do I have to look this up every time? The info just doesn't stick in my head no matter what... – gloomy.penguin May 09 '13 at 21:47
  • 7
    ^ Welcome to the club. – Rick Bross May 14 '13 at 22:22
  • Hello. But for number like 12.5, 20.3. They return 125 and 203? – Hai Tien Dec 06 '16 at 02:29
  • @MinhAnh - This just just a very basic example for the question at the time. I've added a little bit extra for the case that you've mentioned. – Jonathon Bolster Dec 09 '16 at 23:33
  • @gloomy.penguin pretty sure 90+% of us use cheat sheets, because yeah, VERY complicated regular expressions are. (I was 16 years into my career before I even attempted to get my head around them.) :) – Scott Fraley Aug 01 '17 at 16:16
  • @gloomy.penguin This particular regex could be simpler: `\d*\.?\d+` (optional digits, optional dot, at least one digit). –  Aug 01 '17 at 16:36
  • /(not )?to be/i – gloomy.penguin Aug 01 '17 at 16:38
26

Not really jQuery at all:

number = number.replace(/\D/g, '');

That regular expression, /\D/g, matches any non-digit. Thus the call to .replace() replaces all non-digits (all of them, thanks to "g") with the empty string.

edit — if you want an actual *number value, you can use parseInt() after removing the non-digits from the string:

var number = "number32"; // a string
number = number.replace(/\D/g, ''); // a string of only digits, or the empty string
number = parseInt(number, 10); // now it's a numeric value

If the original string may have no digits at all, you'll get the numeric non-value NaN from parseInt in that case, which may be as good as anything.

Pointy
  • 405,095
  • 59
  • 585
  • 614