【问题标题】:Split string for first two white spaces拆分前两个空格的字符串
【发布时间】:2019-07-23 17:30:02
【问题描述】:

我有这个字符串

var str = "394987011016097814 1d the quick brown fox jumped over the lazy dog";

..我正试图让它成为这个数组

[
    "394987011016097814",
    "1d",
    "the quick brown fox jumped over the lazy fox",
]

我已经看到这个答案 Split string on the first white space occurrence 但这只是第一个空格。

【问题讨论】:

  • 按要求获得输出不需要任何黑魔法。那么到目前为止,您尝试了什么?为什么这(或对链接问题进行一些细微调整的众多答案之一)不是您要求的答案?
  • 您确定要在结果数组的第一个元素末尾添加空格吗?
  • 顺便说一句,它是“敏捷的棕色狐狸跳过懒惰的狗”。 ;)

标签: javascript string split


【解决方案1】:

这样使用正则表达式^(\d+)\s(\S+)\s(.*)

var re = new RegExp(/^(\d+)\s(\S+)\s(.*)/, 'gi');
re.exec('394987011016097814 1d the quick brown fox jumped over the lazy fox');

var re = new RegExp(/^(\d+)\s(\S+)\s(.*)/, 'g');
var [, g1, g2, g3] = re.exec('394987011016097814 1d the quick brown fox jumped over the lazy fox');
console.log([g1, g2, g3]);

【讨论】:

    【解决方案2】:

    您可以通过编写一些代码来实现:

    const str = "394987011016097814 1d the quick brown fox jumped over the lazy fox";
    
    const splittedString = str.split(' ');
    let resultArray = [];
    let concatenedString = '';
    
    for (let i = 0; i < splittedString.length; i++) {
        const element = splittedString[i];
        if (i === 0 || i === 1) {
            resultArray.push(element);
        } else {
            concatenedString += element + ' ';
        }
    }
    
    resultArray.push(concatenedString.substring(0, concatenedString.length - 1));
    
    console.log(resultArray);
    
    // output is: 
    // [ '394987011016097814',
    // '1d',
    // 'the quick brown fox jumped over the lazy fox' ]
    

    【讨论】:

      【解决方案3】:

      let str = "394987011016097814 1d the quick brown fox jumped over the lazy fox";
      let arr = [];             
      str = str.split(' ');     
      arr.push(str.shift());    
      arr.push(str.shift());    
      arr.push(str.join(' '));
      console.log(arr);

      【讨论】:

        【解决方案4】:

        使用拆分和连接进行解构

        var str = "394987011016097814 1d the quick brown fox jumped over the lazy fox";
        var [str1, str2, ...str3] = str.split(' ');
        str3 = str3.join(' ');
        console.log([str1, str2, str3])

        【讨论】:

        • @JeremyJohn 所以接受的答案是来自链接问题的答案之一的变体?
        • @Andreas 在链接的问题中回答哪个?我没看到。
        【解决方案5】:

        来源:Split a string only the at the first n occurrences of a delimiter

        var string = 'Split this, but not this',
            arr = string.split(' '),
            result = arr.splice(0,2);
        
        result.push(arr.join(' ')); // result is ["Split", "this,", "but not this"]
        
        alert(result);

        【讨论】:

          【解决方案6】:

          您可以先拆分所有空间,然后取两个值并连接剩余的值。

          var str = "394987011016097814 1d the quick brown fox jumped over the lazy fox";
          let op = str.split(/[ ]+/g)
          
          let final = [...op.splice(0,2), op.join(' ')]
          
          console.log(final)

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-01-01
            • 2017-06-18
            相关资源
            最近更新 更多