Getting numeric value from a alpha numeric string in javascript
javascript, string
Solution
This is what regexes were made for!
var matches = /\d+$/.exec("Ruby12");
matches[0]; //returns 12
var matches = /\d+$/.exec("sfwfewcsd098");
matches[0]; //returns 098
var matches = /\d+$/.exec("abc"); //matches returns null
Problem
I have a string "RowNumber5", now i want to get the numeric value "5" from that string using Javascript. Note: Numeric value will be always at the end, after alphabets, that means numeric value will never occur in between alphabets. Example - ``` Result45 - Yes Result45Abc - Never ``` I can get this "5" by some thing like this ``` var t = "Ruby12"; var y = parseInt(t.split('').reverse().join("")); if(!isNaN(y)) { y = y.toString().split('').reverse().join(""); } else { y = ""; } console.log(y); ``` Any shot way? or Better approach for this ?