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?

Bálint
  • 4,009
  • 2
  • 16
  • 27

4 Answers4

4

You could use a regex:

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

\D matches everything that's not a digit.

Bálint
  • 4,009
  • 2
  • 16
  • 27
1

you can use regexp

var a = "value is 10 ";
var  num = a.match(/\d+/)[0] // "10"
console.log ( num ) ;
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.

djdduty
  • 274
  • 1
  • 8
0

You do it using regex like that

const  pattern = /\d+/g;

const result = yourString.match(pattern);
Dalorzo
  • 19,834
  • 7
  • 55
  • 102
Ahmed Ali
  • 2,574
  • 2
  • 23
  • 38