【问题标题】:Javascript split string every third space [duplicate]Javascript每隔三个空格分割字符串[重复]
【发布时间】:2017-07-10 06:24:38
【问题描述】:

我想每隔三个空格拆分一次字符串。例如:

var str = 'Lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod tempor incididunt';
//result will be:
var result = ["Lorem ipsum dolor ", "sit amet consectetur ", "adipiscing elit sed ", "do eiusmod tempor ", "incididunt"];

请帮助我。谢谢

【问题讨论】:

  • 这个功能我已经用过了,不一样。我想每隔三个空格分割一次,而不是在某个位置。

标签: javascript jquery


【解决方案1】:

使用正则表达式分割字符串。

var str = 'Lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod tempor incididunt';
var splited = str.match(/\b[\w']+(?:[^\w\n]+[\w']+){0,2}\b/g);
console.log(splited);

正则表达式说明:

 1. \b assert position at a word boundary (^\w|\w$|\W\w|\w\W)

 2. Match a single character present in the list below [\w']+
         \w matches any word character (equal to [a-zA-Z0-9_])

 3. {0,2} Quantifier — Matches between 0 and 2 times

 4. Match a single character not present in the list below [^\w\n]

 5. \w matches any word character (equal to [a-zA-Z0-9_])

 6. \n matches a line-feed (newline) character (ASCII 10)

 7. Match a single character present in the list below [\w']

 8. \w matches any word character (equal to [a-zA-Z0-9_])

 9. ' matches the character ' literally (case sensitive)

 10. \b assert position at a word boundary (^\w|\w$|\W\w|\w\W)

 11. g modifier: global. All matches (don't return after first match)

【讨论】:

  • 请添加正则表达式的解释:)
  • 哇,多么好的正则表达式。谢谢您的回答和解释:)我仍然需要学习如何使用正则表达式。
  • @brk 可能想在正则表达式解释代码框之前添加<!-- language: lang-none -->,这样它就不会奇怪地着色:)
  • @Spella 这比你想象的要容易。去吧! This SO answer 是迄今为止我发现的最好的正则表达式教程,也是我学到的。
  • 非常感谢..我会按照这个教程学习的:)
【解决方案2】:

var str = 'Lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod tempor incididunt';
var splitString = str.match(/(.*?\s){3}/g);
console.log(splitString);

【讨论】:

  • 这么简单..感谢您提供解决方案:)
  • 很好,但它真的应该在最后保留尾随空格吗?
【解决方案3】:

在这里查看https://regex101.com/r/npQt7X/1

const regex = /\b[\w']+(?:[^\w\n]+[\w']+){0,2}\b/g;
const str = `Lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod tempor incididunt`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }

    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
}

【讨论】:

    【解决方案4】:

    正则表达式很好,但很难解释和阅读。

    所以我只有在没有其他解决方案的情况下才使用正则表达式。

    反对这些正则表达式的一个论点是参数的硬编码。

    var str = 'Lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod tempor incididunt';
    
    var splitMyString = (str, splitLength) => {
      var a = str.split(' '), b = [];
      while(a.length) b.push(a.splice(0,splitLength).join(' '));
      return b;
    }
    console.log(splitMyString(str,3));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-16
      • 2011-08-18
      • 1970-01-01
      • 2017-11-10
      • 2021-06-16
      • 2014-12-13
      • 2020-05-23
      • 1970-01-01
      相关资源
      最近更新 更多