Regular Expressions 101

Community Patterns

1...45678...575

Word wrap text

1

Regular Expression
PCRE (PHP <7.3)

/
(((^|\n*).{1,42}?)(\n))|(\n*.{1,42})( |\n|$)
/
gsm

Description

(Directly inspired by https://macromates.com/blog/2006/wrapping-text-with-regular-expressions/ )

This version only splits between words, so lines may wrap earlier than the given maximum.

Passes existing linefeed through "as‐is", so probably not useful for reflowing already wrapped text

Submitted by Tristan - 3 years ago