Skip to content

Instantly share code, notes, and snippets.

@Richienb
Last active November 18, 2021 15:11
Show Gist options
  • Save Richienb/6b9a05b4bfe4d57530559f58a4eecf0d to your computer and use it in GitHub Desktop.
Save Richienb/6b9a05b4bfe4d57530559f58a4eecf0d to your computer and use it in GitHub Desktop.
Wrap words
const string = ``;
const wrapWords = (string, width) => string.replace(new RegExp(`(?![^\\n]{1,${width}}$)([^\\n]{1,${width}})\\s`, 'g'), '$1\n');
console.log(string.split('\n').map(value => wrapWords(value, 80)).join('\n\n'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment