1

I need a method to split a string into an array of smaller strings, spliting it by word count. It is, I'm looking for a function like that:

function cut(long_string, number_of_words) { ... }

var str = "word1 word2 word3 word4 word5 word6 word7 word8 word9 word10 word11 word12";
var arr = cut(str, 3);

In this case, cut should return 4 arrays with 3 words each. I've tried to figure out a regex for String.match() or String.split(), but I don't know how to do it..

1
  • 1
    try using the split function for strings. you could split the string into words using the space as a delimiter and then create an array using the number of word to determine the length Commented May 30, 2012 at 19:12

3 Answers 3

2

First split it by spaces, then chunk the array together.

function cut(input,words) {
    input = input.split(" ");
    var l = input.length, i, ret = [];
    for( i=0; i<l; i+=words) {
        ret.push(input.splice(0,words));
    }
    return ret;
}
Sign up to request clarification or add additional context in comments.

Comments

1

Let's do something crazy:

function cut(str, num){
    return str.match(new RegExp("(?:(?:^|\\s)\\S+){1," + num + "}", "g"));
}

This creates a new RegExp at every run and matches the string. There are probably better solutions in terms of speed, but this is wonderful Mad Science(TM). And it's short.

3 Comments

This leaves trailing spaces in substrings. See my answer for a better way.
I've tested with several strings in Chrome and Firefox (latest versions) and it works perfectly @thg435
@Ivan: resulting substrings contain extra leading spaces (not trailing, my mistake). cut("a b c d", 2) -> ["a b", " c d"]
1

Make a function that splits an array into chunks:

chunk = function(ary, len) {
    var i = 0, res = [];
    for (var i = 0; i < ary.length; i += len)
        res.push(ary.slice(i, i + len));
    return res;
}

and apply this function to the list of words:

var str = "word1 word2 word3 word4 word5 word6 word7 word8 word9 word10 word11";
chunk(str.match(/\w+/g), 3)

str.match(/\w+/g) can be also str.split(/\s+/) depending on how you want to handle non-word characters.

If you only interested to create an array of substrings (and not arrays of words as the question states), here's a solution that doesn't leave trailing spaces in substrings:

str.match(new RegExp("(\\S+\\s+){1," + (num - 1) + "}\\S+", "g"))

returns

["word1 word2 word3", "word4 word5 word6", "word7 word8 word9", "word10 word11 word12"]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.