使用angular 2删除text / csv文件中不必要的逗号

时间:2018-05-18 13:30:27

标签: angular typescript

我想删除文件中不必要的逗号,代码如下:

const data: any = utils.sheet_to_csv(ws);
let blob = new Blob(['\ufeff' + data], { type: 'text;charset=utf-8;' });
let dwldLink = document.createElement("a");
let url = URL.createObjectURL(blob);

我从第一行得到低于输出:

,,,,,       ,NTD00539,100000,01,Varsha,20180607   

如何删除前5个逗号?

1 个答案:

答案 0 :(得分:0)

您可以删除不必要的',',如下所示:

    while (data.match(/^,/) != null) {    
      data = data.replace(/^,/, ""); //replace  comma from firstplace    
    }

    while (data.match(/,\s*$/) != null) {    
       data = data.replace(/,\s*$/, ""); //replace  comma from lastplace    
    }

    while (data.match(/,,/g) != undefined) {    
      data = data.replace(/,,/g, ','); console.log('count')//replace  consecutive occurrence of comma 
    }