49

I have a string in a loop and for every loop, it is filled with texts the looks like this:

"123 hello everybody 4"
"4567 stuff is fun 67"
"12368 more stuff"

I only want to retrieve the first numbers up to the text in the string and I, of course, do not know the length.

Thanks in advance!

simhumileco
  • 31,877
  • 16
  • 137
  • 115
Fred
  • 1,129
  • 1
  • 14
  • 35
  • 2
    If you *know* there's a number in the string, you can use this one-liner: `"Hello 123 there! 45".match(/\d+/).shift();` Otherwise, you'll want to test for `null` before doing the `.shift()`. [Source](https://stackoverflow.com/a/609588/114558) – rinogo Jul 13 '17 at 17:36

5 Answers5

81

If the number is at the start of the string:

("123 hello everybody 4").replace(/(^\d+)(.+$)/i,'$1'); //=> '123'

If it's somewhere in the string:

(" hello 123 everybody 4").replace( /(^.+)(\w\d+\w)(.+$)/i,'$2'); //=> '123'

And for a number between characters:

("hello123everybody 4").replace( /(^.+\D)(\d+)(\D.+$)/i,'$2'); //=> '123'

[addendum]

A regular expression to match all numbers in a string:

"4567 stuff is fun4you 67".match(/^\d+|\d+\b|\d+(?=\w)/g); //=> ["4567", "4", "67"]

You can map the resulting array to an array of Numbers:

"4567 stuff is fun4you 67"
  .match(/^\d+|\d+\b|\d+(?=\w)/g)
  .map(function (v) {return +v;}); //=> [4567, 4, 67]

Including floats:

"4567 stuff is fun4you 2.12 67"
  .match(/\d+\.\d+|\d+\b|\d+(?=\w)/g)
  .map(function (v) {return +v;}); //=> [4567, 4, 2.12, 67]

If the possibility exists that the string doesn't contain any number, use:

( "stuff is fun"
   .match(/\d+\.\d+|\d+\b|\d+(?=\w)/g) || [] )
   .map(function (v) {return +v;}); //=> []

So, to retrieve the start or end numbers of the string 4567 stuff is fun4you 2.12 67"

// start number
var startingNumber = ( "4567 stuff is fun4you 2.12 67"
  .match(/\d+\.\d+|\d+\b|\d+(?=\w)/g) || [] )
  .map(function (v) {return +v;}).shift(); //=> 4567

// end number
var endingNumber = ( "4567 stuff is fun4you 2.12 67"
  .match(/\d+\.\d+|\d+\b|\d+(?=\w)/g) || [] )
  .map(function (v) {return +v;}).pop(); //=> 67
KooiInc
  • 119,216
  • 31
  • 141
  • 177
  • These dont work very well. ("hello123").replace( /(^.+\D)(\d+)(\D.+$)/i,'$2') returns "hello124". In fact, none of these work if the number is at the end of the string. – dooderson Oct 15 '14 at 18:23
  • 3
    @user1311069: well, that situation was not included in the answer. It would be constructive if you provided your solution to it. Feel free ;) – KooiInc Oct 15 '14 at 20:10
  • 1
    @user1311069: provided a few extra methods, satisfying you needs too. – KooiInc Oct 16 '14 at 08:33
  • 5
    These are way too complicated. `"Hello 123 there! 45".match(/\d+/).shift();` works for most needs. Test for `null` before the `.shift()` if you're not sure if there's a number in the string. – rinogo Jul 13 '17 at 17:38
54
var str = "some text and 856 numbers 2";
var match = str.match(/\d+/);
document.writeln(parseInt(match[0], 10));

If the strings starts with number (maybe preceded by whitespace), simple parseInt(str, 10) is enough. parseInt will skip leading whitespace.

10 is necessary, because otherwise string like 08 will be converted to 0 (parseInt in most implementations consider numbers starting with 0 as octal).

David d C e Freitas
  • 7,481
  • 4
  • 58
  • 67
Eugene Morozov
  • 15,081
  • 3
  • 25
  • 32
  • 7
    Given the regular expression `/\d+/` you don't really need `parseInt`, something like `+match.shift()` or `Number(match.shift())` is sufficient. Furthermore, you need to check if there actually is a match. The input string may not contain numbers at all: in that case `match` will be `null`. – KooiInc Oct 16 '14 at 08:51
10

If you want an int, just parseInt(myString, 10). (The 10 signifies base 10; otherwise, JavaScript may try to use a different base such as 8 or 16.)

Sophie Alpert
  • 139,698
  • 36
  • 220
  • 238
5

This replace method with a simple regular expression ([^\d].*):

'123 your 1st string'.replace( /[^\d].*/, '' );
// output: "123"

remove everything without the first digits.

simhumileco
  • 31,877
  • 16
  • 137
  • 115
3

Use Regular Expressions:

var re = new RegExp(/^\d+/); //starts with digit, one or more
var m = re.exec("4567 stuff is fun 67");
alert(m[0]); //4567

m = re.exec("stuff is fun 67");
alert(m); // null
Spikolynn
  • 4,067
  • 2
  • 37
  • 44