歌词解析排序

1.解析歌词代码如下:


let str=`[01:33.50]1111
[02:33.50]asas2222
[03:33.50]3333
[01:35.50][03:33.51] 44[]44
[01:36.50] [03:33.52] [03:33.56]55 55
[01:35.52] [03:33.53] 666`


str=str.replace(/([d{2}:d{2}.d{2}]s?)+(.+)/g,function($,$1,$2){
    console.log($,'--------',$1,'--------',$2);
    if($.length == $1.length + $2.length){
        return $1+$2;
    }else{
        var str1 = $.split($2)[0].replace(/s/g,''),a = '';
        for(var j = 0;j < str1.length;j++){
            a += str1.substr(j,10) + $2 +'
';
            j += 9;
        }
        return a.substr(0, a.length - 1);

    }
    
})

console.log(str)

arr=str.split('
');
arr.sort((a,b)=>{
    return a.substr(1,8).replace(/[:.]/g,'')-b.substr(1,8).replace(/[:.]/g,'');
})

console.log(arr)

2.打印结果如下:

// 正则打印结果
[01:33.50]1111 -------- [01:33.50] -------- 1111
[02:33.50]asas2222 -------- [02:33.50] -------- asas2222
[03:33.50]3333 -------- [03:33.50] -------- 3333
[01:35.50][03:33.51] 44[]44 -------- [03:33.51]  -------- 44[]44
[01:36.50] [03:33.52] [03:33.56]55 55 -------- [03:33.56] -------- 55 55
[01:35.52] [03:33.53] 666 -------- [03:33.53]  -------- 666

// 正则处理后的结果
[01:33.50]1111
[02:33.50]asas2222
[03:33.50]3333
[01:35.50]44[]44
[03:33.51]44[]44
[01:36.50]55 55
[03:33.52]55 55
[03:33.56]55 55
[01:35.52]666
[03:33.53]666 

// 排序后结果

[01:33.50]1111
[01:35.50]44[]44
[01:35.52]666
[01:36.50]55 55
[02:33.50]asas2222
[03:33.50]3333
[03:33.51]44[]44
[03:33.52]55 55
[03:33.53]666
[03:33.56]55 55
原文地址:https://www.cnblogs.com/yuesu/p/10749907.html