将字符串拆分为每个索引n个字的数组

时间:2022-03-07 01:48:06

I have a string that I'd like to split in an array that has (for example) 3 words per index. What I'd also like it to do is if it encounters a new line character in that string that it will "skip" the 3 words limit and put that in a new index and start adding words in that new index until it reaches 3 again. example

我有一个字符串,我想分成一个数组,每个索引有(例如)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"]

I've tried looking into regular expressions, but so far I can't really make sense of the syntax that is used in regex.

我已经尝试过查看正则表达式,但到目前为止,我无法理解正则表达式中使用的语法。

I have written a way to complicated function that splits my string into lines of 3 by first splitting it into an array of separate words using .split(" "); and then using a loop to add add it per 3 into another array. But with that I can't take the new line character into account.

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

5 个解决方案

#1


4  

You can try with this pattern:

您可以尝试使用此模式:

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

since the quantifier {0,2} is greedy by default, it will take a value less than 2 (N-1) only if a newline is found (since newlines are not allowed here: [^\w\n]+) or if you are a the end of the string.

因为量词{0,2}默认是贪婪的,所以只有在找到换行符时才会取小于2(N-1)的值(因为这里不允许换行:[^ \ w \ n] +)或如果你是字符串的结尾。

#2


2  

If you're interested in a regexp solution, it goes like this:

如果您对正则表达式解决方案感兴趣,它会像这样:

   text.match(/(\S+ \S+ \S+)|(\S+ \S+)(?= *\n|$)|\S+/g)
   // result ["this is some", "text that I'm", "typing here", "yes I really", "am"]

Explanation: match either three space separated words, or two words followed by spaces + newline, or just one word (a "word" being simply a sequence of non-spaces).

说明:匹配三个空格分隔的单词,或两个单词后跟空格+换行符,或只匹配一个单词(“单词”只是一个非空格序列)。

For any number of words, try this:

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

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

(replace N-1 with a number).

(用数字代替N-1)。

#3


1  

Try something like this:

尝试这样的事情:

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);

Basically what this does is splits the string by words, then loops through each word. Every third word it gets to, it adds that along with what is stored in temp into the array, otherwise it adds the word to temp.

基本上它的作用是按字分割字符串,然后循环遍历每个单词。它得到的每三个单词,它将存储在temp中的内容添加到数组中,否则它将单词添加到temp。

#4


0  

Only if you know there are no words 'left', so the number of words is always a multiple of 3:

只有当你知道没有'左'字时,所以单词的数量总是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"]

but if you add a word:

但是如果你添加一个单词:

"this is some text that I'm typing here \n yes I really am FOO".match(/\S+\s+\S+\s+\S+/g)

the result will be exactly the same, so "FOO" is missing.

结果将完全相同,因此缺少“FOO”。

#5


0  

here one more way: use this pattern ((?:(?:\S+\s){3})|(?:.+)(?=\n|$))
Demo

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

#1


4  

You can try with this pattern:

您可以尝试使用此模式:

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

since the quantifier {0,2} is greedy by default, it will take a value less than 2 (N-1) only if a newline is found (since newlines are not allowed here: [^\w\n]+) or if you are a the end of the string.

因为量词{0,2}默认是贪婪的,所以只有在找到换行符时才会取小于2(N-1)的值(因为这里不允许换行:[^ \ w \ n] +)或如果你是字符串的结尾。

#2


2  

If you're interested in a regexp solution, it goes like this:

如果您对正则表达式解决方案感兴趣,它会像这样:

   text.match(/(\S+ \S+ \S+)|(\S+ \S+)(?= *\n|$)|\S+/g)
   // result ["this is some", "text that I'm", "typing here", "yes I really", "am"]

Explanation: match either three space separated words, or two words followed by spaces + newline, or just one word (a "word" being simply a sequence of non-spaces).

说明:匹配三个空格分隔的单词,或两个单词后跟空格+换行符,或只匹配一个单词(“单词”只是一个非空格序列)。

For any number of words, try this:

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

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

(replace N-1 with a number).

(用数字代替N-1)。

#3


1  

Try something like this:

尝试这样的事情:

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);

Basically what this does is splits the string by words, then loops through each word. Every third word it gets to, it adds that along with what is stored in temp into the array, otherwise it adds the word to temp.

基本上它的作用是按字分割字符串,然后循环遍历每个单词。它得到的每三个单词,它将存储在temp中的内容添加到数组中,否则它将单词添加到temp。

#4


0  

Only if you know there are no words 'left', so the number of words is always a multiple of 3:

只有当你知道没有'左'字时,所以单词的数量总是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"]

but if you add a word:

但是如果你添加一个单词:

"this is some text that I'm typing here \n yes I really am FOO".match(/\S+\s+\S+\s+\S+/g)

the result will be exactly the same, so "FOO" is missing.

结果将完全相同,因此缺少“FOO”。

#5


0  

here one more way: use this pattern ((?:(?:\S+\s){3})|(?:.+)(?=\n|$))
Demo

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