admin管理员组文章数量:1319471
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
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 plicated 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.
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
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 plicated 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.
-
You can't split with that magic. Loop over the string, count the spaces,
push
every 3rd space, and\n
and there you go. Custom split – Sterling Archer Commented May 11, 2014 at 19:10
5 Answers
Reset to default 4You 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.
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).
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
.
Only if you know there are no words 'left', so the number of words is always a multiple of 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.
here one more way:
use this pattern ((?:(?:\S+\s){3})|(?:.+)(?=\n|$))
Demo
本文标签: javascriptsplit string into array of n words per indexStack Overflow
版权声明:本文标题:javascript - split string into array of n words per index - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742060122a2418531.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论