【问题标题】:Node separate the lines in data separated by a semi-colon节点分隔数据中的行,用分号分隔
【发布时间】:2019-09-23 00:26:03
【问题描述】:

我有一些看起来像下面的数据,每行的末尾用分号分隔。

id=0345 f1() status=1;
id=7645 f2() status=3;

如何将每一行放入一个数组中(没有结束分号)。

我试过了:

var arr = data.split(';');
console.log(arr);

但是没有用。

我该怎么做?

【问题讨论】:

    标签: javascript arrays node.js string


    【解决方案1】:

    只用分号和换行符分割:

     
    
    const str = `id=0345 f1() status=1;
    id=7645 f2() status=3;`
    const arr = str.split(";\n");
    console.log(arr);

    【讨论】:

      【解决方案2】:

      您可以删除每行末尾的所有;,然后在\n 处删除split

      const str = `id=0345 f1() status=1;
      id=7645 f2() status=3;`
      
      const arr = str.replace(/;$/gm, '').split(/\n/g)
      
      console.log(arr)

      【讨论】:

        猜你喜欢
        • 2015-12-21
        • 1970-01-01
        • 1970-01-01
        • 2017-08-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-19
        • 2013-06-07
        相关资源
        最近更新 更多