Word Wrap

Wrap text to specified line width.

Code

General
text.replace(new RegExp(`(?![^\\n]{1,${width}}$)([^\\n]{1,${width}})\\s`, 'g'), '$1\n');

Parameters

Text to wrap

Maximum line width

Browser·fetch() may be limited by CORS

More JavaScript Snippets