Andrew Andrew - 5 months ago 11
Javascript Question

How to extract last digit from string which contains chars and numbers

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);

Answer

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

console.log(lastNumber);