Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the first integers in a string with JavaScript

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!

like image 786
Fred Avatar asked Mar 04 '09 07:03

Fred


People also ask

How do you get the first digit of an int in JavaScript?

To get the first digit of a number:Convert the number to a string. Access the string at index 0 , using square brackets notation e.g. String(num)[0] . Convert the result back to a number to get the first digit of the number.

How do you get the first character in a string JavaScript?

You should use the charAt() method, at index 0, to select the first character of the string. NOTE: charAt is preferable than using [ ] (bracket notation) as str. charAt(0) returns an empty string ( '' ) for str = '' instead of undefined in case of ''[0] .

How do you find a number in a string?

The number from a string in javascript can be extracted into an array of numbers by using the match method. This function takes a regular expression as an argument and extracts the number from the string. Regular expression for extracting a number is (/(\d+)/).

How do you find the first digit of a number?

To finding first digit of a number is little expensive than last digit. To find first digit of a number we divide the given number by 10 until number is greater than 10.


2 Answers

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 
like image 70
KooiInc Avatar answered Sep 29 '22 23:09

KooiInc


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).

like image 36
Eugene Morozov Avatar answered Sep 29 '22 21:09

Eugene Morozov