/x (Extended / Verbose)
What is /x (Extended / Verbose)?
/x (Extended / Verbose): Allow whitespace and # comments inside the pattern for readability. Not available in JavaScript. The x flag, called extended or verbose mode, lets you add whitespace and comments inside a regular expression. Spaces are ignored and # starts a comment, so you can lay a pattern out over several lines and explain each part.
Frequently asked questions about /x (Extended / Verbose)
How is /x (Extended / Verbose) used in practice?
It is available in PHP, Python, Perl, Ruby and .NET, but not in JavaScript. A postcode or date pattern that is unreadable on one line becomes understandable when each piece has its own line and a short note.
What should you keep in mind with /x (Extended / Verbose)?
Remember that literal spaces now need escaping, as \ or [ ], or they are ignored. If a regex is long enough that you are tempted to explain it in a comment above the code, extended mode is usually the better place for that explanation. Future readers, including you in six months, will thank you.
At a glance
| Category | Regex flags |
|---|---|
| Explained by | Reinier de la Parra |
| Updated |