Last Updated: February 25, 2016
·
526
· 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?

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside