Make it possible to divide regular expressions into chunks

Regular expressions are often hard to read.

After reading an article about verbose regular expressions in Python, and how one could do something like this in ECMAScript, I realised that there is one really simple way how this could be done:

Simply concatenate multiple chunks, which are separated by whitespace, back into one. So /expr1/ /expre2/ is read and executed as /expr1expr2/.

This way it becomes much more readable and we can also easily add comments:

new RegExp(
	/expr1/ // comments on expr1
	/expr2/ // comments on expr2
	, 'i' )

AFAIK this shouldn't be to hard to do.

Doing this with syntax (two regex literals directly next to each other) is actually very difficult, due to the similarities with infix division operator. Actually, just parsing a single regex is difficult due to division operator.

There's a related regex feature called free spacing that would allow just this. It hasn't been discussed in a while, though. Last mention I can find is Allow spaces in curly brackets in RegularExpressions

1 Like

Thanks for explaining.

I hoped it would be simple. If there is some code that recognizes a regex expression then it may not be to hard to repeat it until no expressions are found.

Free spacing looks like what Verbose Regular Expressions are in Python.

It's a pitty, but we can still do strings that can be broken up and converted to a regular expression.

@jridgewell What about something like @/multline regexp/flags or some variant thereof (using a symbol that's not a valid binary operator)?