I'm trying to get the last String (from the right hand side) of the cells in a column but I cant figure it out.
I have this code that would get the first three digits from the left hand side but yeah that wont work.
var first_3_digs = values.filter(r => {
if(r.toString().includes('_')){return r;}
}).map(r=> r.toString().split('_')[0]);
For example if I want as an input CS_1*44u i want the output u. Same for ml, l etc...
If you need more info please let me know.
Thank you so much for the help.
CodePudding user response:
Example how to get the 'units' from several cells:
const unit = x => /[\d] [\D] /.test(x) ? x.match(/[A-z] $/)[0] : '';
var texts = ['CS_1*44u', 'Case', '72x64ml', '11x37kg', '123'];
texts.forEach(x => console.log(unit(x))); // --> 'u', '', 'ml', 'kg', ''
Example how to get the 'unit' from one cell:
const unit = x => /[\d] [\D] /.test(x) ? x.match(/[A-z] $/)[0] : '';
var cell_value = 'CS_1*44u';
console.log(unit(cell_value)); // --> 'u'
If you need to get the array of the 'units' from another array of cells you can map the function this way:
const unit = x => /[\d] [\D] /.test(x) ? x.match(/[A-z] $/)[0] : '';
var cells = ['CS_1*44u', 'Case', '72x64ml', '11x37kg', '123'];
var units = cells.map(x => unit(x)); // <---- here
console.log(units); // --> ['u', '', 'ml', 'kg', '']
CodePudding user response:
as you have tagged formulas for possible solution, i will offer the following REGEXEXTRACT formula:
=REGEXEXTRACT(K1,"[^(\d (\.\d )?)(?!.*\d (\.\d )?)]*$")
explanation:
AFTER last occurrence of "a":
[^a]*$
LAST digit (with or without decimal):
(\d (\.\d )?)(?!.*\d (\.\d )?)
[EDIT]
where \d means a number, here is a (shorter) alternative which is also tested as working with the same dataset:
=REGEXEXTRACT(K1,"[^\d]*$")


