This question has already been answered:
There is a string
"!(31310"
How to convert it to number?
parseInt(), new Number(), valueOf() - не подходят
This question has already been answered:
There is a string
"!(31310"
How to convert it to number?
parseInt(), new Number(), valueOf() - не подходят
A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .
like so
var string = "!(31310"; var number = +(string.match(/\d+/g)); console.log(typeof number); console.log(number);
what it does, match collects all the numbers into an array and stitches the array with join, and with + we convert the value from the String type to the Number type
\d+
, and remove join
- Grundyfor integers, the solution is for example:
function isNumeric(n) { return !isNaN(parseFloat(n)) && isFinite(n); } function getIntFromString(str) { var str_num = ""; var i = 0; while (i < str.length && !isNumeric(str[i]) && str[i] !== '-') { i++; } if (str[i] === '-') { str_num += '-'; } i = 0; while (i < str.length) { str_num += isNumeric(str[i]) ? str[i] : ''; ++i; } return parseInt(str_num); } var str = "!(31310"; var num = getIntFromString(str); alert(typeof num); alert(num);
Source: https://ru.stackoverflow.com/questions/542036/
All Articles