archives

« Bugzilla Issues Index

#4423 — Support comment tokens in regular expressions


Regular expressions can become increasingly hard to maintain, especially if the language does not allow for commenting on what you're trying to achieve.

Thus, some languages (e.g. Perl, Python and even PHP) implement an additional RegExp token that can contain comments. More specifically, you can write (?# your comments like this), so EcmaScript regular expressions like these:

/(?=^[^%\\#]+$)(?=^\S.*$)(?=^.*\S$).*/

could be commented as such:

/(?#
Exclude %, \ and # characters

)(?=^[^%\\#]+$)(?#
Make sure that the string starts with a non-whitespace character

)(?=^\S.*$)(?#
Make sure that the string ends in a non-whitespace character

)(?=^.*\S$)(?#
If all previous lookaheads were successful, match everything

).*/

Note that the latter expression is still a valid regular expression equivalent to the above.