0
How get value numerals from x= "11" that was x=11 with help JavaScript
2 Answers
+ 2
I believe you are asking how to cast a string datatype into an integer
Javascript has a built in function called parseInt which allows you to do this
Example:
var x = '11';
var y = parseInt(x);
y now equals 11
+ 9
You can use a regex to get the first integer :
var num = parseInt(str.match(/\d+/),10)
If you want to parse any number (not just a positive integer, for example "asd -98.43") use
var num = str.match(/-?\d+\.?\d*/)
Now suppose you have more than one integer in your string :
var str = "a24b30c90";
Then you can get an array with
var numbers = str.match(/\d+/g).map(Number);
Result :Â [24, 30, 90]