问题描述:

How can i extract from string only last number which should be "5"?

var str = "1000040928423195 points added to your balance";

var str = parseInt(str);

var lastNum = str.substr(str.length - 1);

console.log(lastNum);

网友答案:

var str = "1000040928423195 points added to your balance";
var lastNumber = str.replace(/\D/g,'');
lastNumber = lastNumber.substring(lastNumber.length - 1);

console.log(lastNumber);
网友答案:

Just add toString() after parseInt

var str = "1000040928423195 points added to your balance";
var str = parseInt(str).toString();
var lastNum = str.substr(str.length - 1);
console.log(lastNum);
网友答案:

Use a regex like /\d+(\d)/

var str = "1000040928423195 points added to your balance";
str.match(/\d+(\d)/)[1]
网友答案:

After your second line, str isn't a string any more; it is an integer. As such, you can extract the last digit with str % 10; if necessary, convert that to a string.

网友答案:

Try parsing it with a Regular Expression like so :)

string = "120394023954932503 fdsaf fdsaf dasfasd";
regex = /([0-9]*)/g;
match = regex.exec(string);
last_num = match[0].substr(match[0].length - 1);
console.log(last_num);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="string_val"></div>
网友答案:

Use the regex \d+(\d) to match multiple digits followed by a digit.

\d will occupy as many as possible, where (\d) will match the last.

Then get the last number from the first group [1].

var str = "1000040928423195 points added to your balance";

var match = /\d+(\d)/g.exec(str)[1];

console.log(match);
网友答案:
var digit = /.*(\d)/.exec("awoo123awoo")[1];

3

相关阅读:
Top