以字母排序時, 會遇到排成 chr1, chr17, chr9 , 如果想排成 chr1,chr9,ch17 則需要取出內容再轉成數字, 解法:
let a = "chr17_gl0000204";
let matches = str.match(/\d+/g);
// ['17', '0000204']
How to split alphanumeric string but keep last alpha value javascript:
string.match(/[a-z]+|\d+/ig)
完整範例:
function isNumeric(value) {
return /^-?\d+(\.\d+)?$/.test(value);
}
function format_alpha_to_num(s) {
let new_s = [];
if(s.length) {
let num_matches = (s).match(/[a-zA-Z]+|\d+/g);
if(num_matches) {
num_matches.forEach((element) => {
if(isNumeric(element)) {
let t = "00000000" + element;
new_s.push(t.substring(t.length-8, t.length));
} else {
new_s.push(element);
}
});
}
}
return new_s.join('');
}
sort_value = format_alpha_to_num(label_value);
實際執行畫面比較: