Javascript 将字符串拆分为每个索引包含n个单词的数组

Javascript 将字符串拆分为每个索引包含n个单词的数组,javascript,arrays,regex,string,Javascript,Arrays,Regex,String,我有一个字符串,我想在一个数组中拆分,该数组(例如)每个索引有3个单词。 我还希望它做的是,如果它在字符串中遇到一个新行字符,它将“跳过”3个单词的限制,并将其放入一个新索引中,然后开始在新索引中添加单词,直到它再次达到3。范例 var text = "this is some text that I'm typing here \n yes I really am" var array = text.split(magic) array == ["this is some", "text

我有一个字符串,我想在一个数组中拆分,该数组(例如)每个索引有3个单词。 我还希望它做的是,如果它在字符串中遇到一个新行字符,它将“跳过”3个单词的限制,并将其放入一个新索引中,然后开始在新索引中添加单词,直到它再次达到3。范例

var text = "this is some text that I'm typing here \n yes I really am"

var array = text.split(magic)

array == ["this is some", "text that I'm", "typing here", "yes I really", "am"]
我试过研究正则表达式,但到目前为止,我还不能真正理解正则表达式中使用的语法


我已经编写了一个复杂函数的方法,通过首先使用
.split(“”)将字符串拆分为一个单独的单词数组,将字符串拆分为3行
然后使用循环将其每3个添加到另一个数组中。但是有了这些,我就不能考虑新行字符了。

试试这样的方法:

words = "this is some text that I'm typing here \n yes I really am".split(" ");
result = [];
temp = "";

for (i = 0; i < words.length; i++) {
  if ((i + 1) % 3 == 0) {
    result.push(temp + words[i] + " ");
    temp = "";
  } else if (i == words.length - 1) {
    result.push(temp + words[i]);
  } else {
    temp += words[i] + " ";
  }
}

console.log(result);
   text.match(/(\S+ \S+ \S+)|(\S+ \S+)(?= *\n|$)|\S+/g)
   // result ["this is some", "text that I'm", "typing here", "yes I really", "am"]
words=“这是我在这里输入的一些文本\n是的,我真的是”。split(“”);
结果=[];
温度=”;
对于(i=0;i

基本上,它是将字符串按单词拆分,然后循环遍历每个单词。每找到第三个单词,它就会将该单词与存储在
temp
中的内容一起添加到数组中,否则它会将该单词添加到
temp

如果您对regexp解决方案感兴趣,它会这样做:

words = "this is some text that I'm typing here \n yes I really am".split(" ");
result = [];
temp = "";

for (i = 0; i < words.length; i++) {
  if ((i + 1) % 3 == 0) {
    result.push(temp + words[i] + " ");
    temp = "";
  } else if (i == words.length - 1) {
    result.push(temp + words[i]);
  } else {
    temp += words[i] + " ";
  }
}

console.log(result);
   text.match(/(\S+ \S+ \S+)|(\S+ \S+)(?= *\n|$)|\S+/g)
   // result ["this is some", "text that I'm", "typing here", "yes I really", "am"]
说明:匹配三个空格分隔的单词,或者两个单词后跟空格+换行符,或者只匹配一个单词(“单词”只是一个非空格序列)

对于任意数量的单词,请尝试以下操作:

text.match(/((\S+ ){N-1}\S+)|(\S+( \S+)*)(?= *\n|$)|\S+/g)

(用数字替换
N-1

您可以尝试以下模式:

var result = text.match(/\b[\w']+(?:[^\w\n]+[\w']+){0,2}\b/g);

由于量词
{0,2}
在默认情况下是贪婪的,因此只有在找到换行符时(因为此处不允许换行:
[^\w\N]+
),或者如果您是字符串末尾的一个从句时,它才会接受小于2(N-1)的值。

只有在您知道没有单词“left”时,因此单词数始终是3的倍数:

"this is some text that I'm typing here \n yes I really am".match(/\S+\s+\S+\s+\S+/g)
=> ["this is some", "text that I'm", "typing here \n yes", "I really am"]
但如果你加上一个词:

"this is some text that I'm typing here \n yes I really am FOO".match(/\S+\s+\S+\s+\S+/g)
结果将完全相同,因此缺少“FOO”。

这里还有一种方法: 使用此模式
((?:(?:\S++\S){3})(?:。+)(?=\n |$)


你不能用这种魔法分裂。在字符串上循环,计算空格,
每三个空格按一次,然后
\n
就到了。请解释你的答案这很接近。我遇到的唯一问题是,它在使用特殊字符(如
?#!@&。有时它只是将这些内容从字符串中删除。我猜这与
\w
有关,因为这只包括[a-zA-Z0-9]?