I have couple of strings which I want to separate out the numbers from last position. If the string doesn't end with a number, 0 or false should be returned.
Here are some examples:
"A1C10" should return 10"20AX23" should return 23"15AZC" should return 0 or falseYou could use match here:
var inputs = ["A1C10", "20AX23", "15AZC"];
var outputs = inputs.map(x => x.match(/\d+$/) ? x.match(/\d+$/)[0] : "0");
console.log(outputs);
For your new requirement, we can split the input on (?<=\D)(?=\d*$):
var inputs = ["A1C10", "20AX23", "15AZC"];
for (var i=0; i < inputs.length; ++i) {
var parts = inputs[i].split(/(?<=\D)(?=\d*$)/);
if (parts.length == 1) parts.push("0");
console.log(inputs[i] + " => " + parts);
}