Created
May 7, 2018 08:55
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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