Last Updated: February 25, 2016
·
394
· arnaudrinquin

Split a text after a maximum of X words

This function does the work.

function splitAfterXWords(to_split, words){
    regex = new RegExp("(([\\s;:!,.?\"'’]*[^\\s]+){" + words + "})(.*)")
    result = regex.exec(to_split)
    return result ? [result[1], to_split.substr(result[1].length)] : [to_split, '']
}

http://stackoverflow.com/questions/19517228/split-a-text-after-a-maximum-of-x-words

Any improvement?