将格式转换为新格式的javascript

时间:2018-03-30 12:04:09

标签: javascript

26(5p) => 19005(3p)
6827(3p) => 6939(3p)

箭头转换为'-'

将此格式替换为

#slices= '5p.26-3p.19005,3p.6827-3p.6939' 

使用JavaScript

我尝试了以下代码,执行时间太长

for(let i=0; i<this.selectedSliceMulti.length; i++)
{
    let replacevalue = this.slicesList[0].replace(/\=>/,'');
    let replacevalue2 = replacevalue.replace(/ /g,'');
    let replacevalue3 = replacevalue2.split(/[(\)]/);
    replacevalue3.splice(4);
    let halfWayThough = Math.floor(replacevalue3.length / 2)
    let arrayFirstHalf = replacevalue3.slice(0, halfWayThough);
    let arraySecondHalf = replacevalue3.slice(halfWayThough, replacevalue3.length);
    console.log(replacevalue3)
    //this.slicesList[0].replace('\=>\','-')
}

1 个答案:

答案 0 :(得分:3)

您可以使用捕获组替换单个正则表达式:

&#13;
&#13;
var arr = ["26(5p) => 19005(3p)", "6827(3p) => 6939(3p)"];

var newArr = arr.map(str => str.replace(/(\d+)\((\w+)\) => (\d+)\((\w+)\)/, '$2.$1-$4.$3'));
var result = newArr.join(",");
console.log(result);
&#13;
&#13;
&#13;