2

So I have the following string

var str = "Ana has 27 apples";

Is there any function that can return only the number 27 from str as an integer ?

I have found an old thread here about something like this but it was about Java. However it got me into Regular Expressions.

And I went ahead and tried playing with this but nothing gone as I expected.. This is the last expression I tried before coming here:

 var str = "Ana has 27 apples";
 var regex = /([0-9])\d+/;
 var ret = str.match(regex);
 console.log(ret);

And it returns 27,2 but I don't want that 2 returned and I definitely do not want it as a string. The thing is that the number is not necessary 27 , its a given number and it can be any integer.

So can it be done with Regular Expressions or is there another ("better") way of doing it without supplimentary lines of code?

Vikasdeep Singh
21.9k16 gold badges82 silver badges106 bronze badges
asked Jun 29, 2018 at 15:05
2
  • If you want to extract a string from a string, and convert it to a numeric value, you have to first extract it, then convert it--there's no way around that. The functionality can be wrapped up in a function so the mainline code doesn't have to think about it. Right now you're capturing two chunks of integers, but I don't know why you'd do that. Commented Jun 29, 2018 at 15:07
  • You're not getting a string, you're getting an array containing two values. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… says: "If the string matches the expression, it will return an Array containing the entire matched string as the first element, followed by any results captured in parentheses. If there were no matches, null is returned." /\d+/g will match multiple sets of numbers in the string and return the results, again in an array - see jsfiddle.net/bqf8cvt7/4 . You can convert each one using parseInt if required. Commented Jun 29, 2018 at 15:09

7 Answers 7

4

And it returns 27,2 but I don't want that 2 returned

See the documentation for match:

it will return an Array containing the entire matched string as the first element, followed by any results captured in parentheses

So the 27 is the whole match and the 2 is from the capture group [0-9].

You probably want /(\d+)/ instead (and then to extract the match from the array with ret[1])

I definitely do not want it as a string.

That's what parseInt, parseFloat and Unary plus + are for.

answered Jun 29, 2018 at 15:10
Sign up to request clarification or add additional context in comments.

Comments

2

You can use parseInt in order to convert numeric string to number;

const str = "Ana has 27 apples";
const num = str.match(/\d+/)[0];
console.log(parseInt(num, 10))

answered Jun 29, 2018 at 15:09

Comments

2

Here you go:

match(/\d+/)[0] regex will help you to get number as string and then use parseInt() to get an integer.

Check below working example:

var str = "Ana has 27 apples";
var num = str.match(/\d+/)[0];
console.log(parseInt(num));

answered Jun 29, 2018 at 15:07

Comments

2

A different way to do it from what was provided by the other answers is to just remove all non numeric characters from the string, then what you are left with will be anything numeric.

function numericOnly(str) {
 return parseInt(str.replace(/\D+/g,''));
}

Note that this is only useful if you know there is only one number in the string. some example results:

console.log(numericOnly('Ana has 27 apples')); // 27
console.log(numericOnly('Ana has 27 apples and 42 oranges')); //2742
console.log(numericOnly('Ana has no apples')); // NaN
answered Jun 29, 2018 at 15:20

Comments

1

Add g on your REGEXP for a global match

var regex = /([0-9])\d+/g;

Hope this helps :>

var str = "Ana has 27 apples";
var regex = /([0-9])\d+/g;
var ret = str.match(regex);
console.log(ret)

answered Jun 29, 2018 at 15:09

Comments

1

Just modify your code like below

You will get 27 only

var str = "Ana has 27 apples";
 var regex = /\d+/;
 var ret = str.match(regex);
 console.log(ret);
answered Jun 29, 2018 at 15:26

Comments

1

You can just do /(\d+)/g which will return all integer groupings.

https://regexfiddler.com/e/hj7dmrxfba81/extract-integers

answered Jun 30, 2018 at 6:44

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.