0

I have a string that looks like:

var a = "value is 10 ";

How would I extract just the integer 10 and put it in another variable?

4
  • Is always the value equals 10? or it maybe be another? Commented Dec 16, 2016 at 22:05
  • Always the same format? "value is x"? Commented Dec 16, 2016 at 22:05
  • no the value will not always be 10 Commented Dec 16, 2016 at 22:06
  • Please use the search. Commented Dec 16, 2016 at 22:28

4 Answers 4

4

You could use a regex:

var val = +("value is 10".replace(/\D/g, ""));

\D matches everything that's not a digit.

Sign up to request clarification or add additional context in comments.

Comments

1

you can use regexp

var a = "value is 10 ";
var  num = a.match(/\d+/)[0] // "10"
console.log ( num ) ;

Comments

1

You can use some string matching to get an array of all found digits, then join them together to make the number as a string and just parse that string.

parseInt(a.match(/\d/g).join(''))

However, if you have a string like 'Your 2 value is 10' it will return 210.

Comments

0

You do it using regex like that

const  pattern = /\d+/g;

const result = yourString.match(pattern);

1 Comment

const is JS version specific not supported by all browsers

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.