'How to get the first string's from the right hand side
So I have this code that would get the first string's from the right hand side and stop whenever there is an integer but for some reason its not working with me.
Example input of fUnit is "CS_25x2u"
expected output of it after using unit is "u".
Real output is "undefined".
function buildUnit(fUnit){// wahts ginna be passed here is the gibirish unit and the output of this function is the clear unit
fUnit = fUnit.toString;
const regex = /[a-zA-Z]*$/;
const unit = (x) => x.match(regex)[0];
fUnit = unit(fUnit);
If you need more info please let me know
Thank you
Solution 1:[1]
const regex = /[a-zA-Z]*$/;
console.log(regex.exec(sample));
Assuming fUnit variable contains your string
const unit = (x) => x.match(regex)[0];
console.log(unit(fUnit));
Solution 2:[2]
Just in case.
function buildUnit(fUnit) {
return fUnit.toString().match(/[A-z]*$/)[0];
}
console.log(buildUnit('CS_25x2u')); // 'u'
console.log(buildUnit('')); // ''
console.log(buildUnit(123)); // ''
console.log(buildUnit('aaa123')); // ''
console.log(buildUnit('aaa 123 bbb')); // 'bbb'
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Yuri Khristich |
