I am a newbie, and have a pile of data strings (sentences). I’m trying to divide each sentence into substrings where each string’s length does not exceed the length of longest single word in that sentence, and returning all words in their original sequence with line breaks (Photoshop carriage return, “r”) dividing the substrings, for that sentence. The words in each string are not hyphenated (only full words or groups of words broken at where a space would be*).* Edit: using the maximum characters, up to the longest word’s character count… so a line would have 2 or 3 words possibly, up to the length of the longest word.
I’ve found examples that split and count the array of words, order them by character length, or add line breaks at set characters, all spaces, etc. But none where I know enough to see an easy modification for this outcome. Any help is greatly appreciated.
2
Answers
Using replace you can replace all the spaces with
n
to get the result.Try using
.split
with a regex:If you want a more direct way:
ES6: