Skip to content

Instantly share code, notes, and snippets.

@GiovanniFrigo
Created May 7, 2018 08:55
Show Gist options
  • Save GiovanniFrigo/23c7c096befa8dd23739f43cd4479ae4 to your computer and use it in GitHub Desktop.
Save GiovanniFrigo/23c7c096befa8dd23739f43cd4479ae4 to your computer and use it in GitHub Desktop.
Simple function to break a sentence around a certain position without breaking the words (only at whitespaces)
function breakSentence(sentence, len) {
const r = new RegExp(`.{1,${len}}[^\\s]*`,'g');
let chunk;
let splitted = [];
while ((chunk = r.exec(sentence)) !== null) {
splitted.push(chunk[0].trim());
}
return splitted;
}
l.replace(/.{1,24}[^\s]*/g, l => a.push(l.trim()))
function breakSentence(sentence, len) {
const r = new RegExp(`.{1,${len}}[^\\s]*`,'g');
let splitted = [];
sentence.replace(r, chunk => splitted.push(chunk.trim()));
return splitted;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment