0

i have string like those / 5 or / 91 or / 358. I need to get the integers after last space from those strings. Here is how I do it know but it only can get one number. How to make it?

f_quantity = f_quantity.substr(f_quantity.length - 1);

3
  • try parseInt or parseFloat Commented Apr 10, 2012 at 14:44
  • @karim79 There is space before / too :P Commented Apr 10, 2012 at 14:45
  • parseInt(f_quantity.split("/")[1],10) then Commented Apr 10, 2012 at 15:16

5 Answers 5

3

Simple example you can validate in a web console:

console.log( (' / 358').match("[0-9]+") );

So...

f_quantity = (' / 358').match("[0-9]+");
Sign up to request clarification or add additional context in comments.

Comments

0

if there is space before the last number you can write the following:

string val = ' / 5' or ' / 91' or ' / 358'. 
var arr = val.split(' ');
arr[val.length-1];

Comments

0

You could use something like this that will get you the number:

var s = " / 91";
var pattern = /[0-9]+/;
var num = s.match(pattern);

Comments

0

Is the string always of the format ' / NUM'? If so, use this to get the string of the number:

f_quantity = f_quantity.substr(3, f_quantity.length);

And then this to turn it into an actual number:

f_quantity = parseInt(f_quantity);

Or as a one-liner:

f_quantity = parseInt(f_quantity.substr(3, f_quantity.length));

Comments

0

This will give you the last integer value preceded by a space, allowing for additional white space at the end:

f_quantity = ~~f_quantity.match( /\s+\d+\s*$/ )

Comments

Your Answer

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.