split string into array of n words per index
arrays, javascript, regex, string
Solution
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.
Problem
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 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.