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?
Written by Arnaud Rinquin
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Javascript
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#