# Make it possible to divide regular expressions into chunks

**URL:** https://es.discourse.group/t/make-it-possible-to-divide-regular-expressions-into-chunks/355
**Category:** 💡 Ideas
**Created:** [June 6, 2020, 1:34pm UTC](https://es.discourse.group/t/make-it-possible-to-divide-regular-expressions-into-chunks/355 "2020-06-06T13:34:48Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![spdev](https://avatars.discourse-cdn.com/v4/letter/s/7cd45c/32.png) [@spdev](https://es.discourse.group/u/spdev)
#### Post date: [June 6, 2020, 1:34pm UTC](https://es.discourse.group/t/make-it-possible-to-divide-regular-expressions-into-chunks/355/1 "2020-06-06T13:34:48Z")

</div>

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:

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

```

AFAIK this shouldn't be to hard to do.

---

<div class="post-metadata">

### Author: ![jridgewell](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/jridgewell/32/18_2.png) [@jridgewell](https://es.discourse.group/u/jridgewell)
#### Post date: [June 8, 2020, 6:00pm UTC](https://es.discourse.group/t/make-it-possible-to-divide-regular-expressions-into-chunks/355/2 "2020-06-08T18:00:44Z")

</div>

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](https://www.regular-expressions.info/freespacing.html) 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](https://es.discourse.group/t/allow-spaces-in-curly-brackets-in-regularexpressions/296/2)

---

<div class="post-metadata">

### Author: ![spdev](https://avatars.discourse-cdn.com/v4/letter/s/7cd45c/32.png) [@spdev](https://es.discourse.group/u/spdev)
#### Post date: [June 16, 2020, 11:32am UTC](https://es.discourse.group/t/make-it-possible-to-divide-regular-expressions-into-chunks/355/3 "2020-06-16T11:32:48Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![claudiameadows](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/claudiameadows/32/126_2.png) [@claudiameadows](https://es.discourse.group/u/claudiameadows)
#### Post date: [June 18, 2020, 1:52am UTC](https://es.discourse.group/t/make-it-possible-to-divide-regular-expressions-into-chunks/355/4 "2020-06-18T01:52:01Z")

</div>

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

---

<div class="post-metadata">

### Author: ![spdev](https://avatars.discourse-cdn.com/v4/letter/s/7cd45c/32.png) [@spdev](https://es.discourse.group/u/spdev)
#### Post date: [July 24, 2020, 12:02pm UTC](https://es.discourse.group/t/make-it-possible-to-divide-regular-expressions-into-chunks/355/5 "2020-07-24T12:02:17Z")

</div>

Just out of curiosity: how does parsing of a regex work?

I assumed (but this maybe naive thinking) that when a forward slash was found, it would just look for the next forward slash that wasn't escaped. And what's in between would then be regarded as a regex.

---

<div class="post-metadata">

### Author: ![bergus](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/bergus/32/152_2.png) [@bergus](https://es.discourse.group/u/bergus)
#### Post date: [July 24, 2020, 12:52pm UTC](https://es.discourse.group/t/make-it-possible-to-divide-regular-expressions-into-chunks/355/6 "2020-07-24T12:52:00Z")

</div>

Except when the forward slash is part of a comment or a division operator. See the details in [ECMA-262 - Ecma International](http://www.ecma-international.org/ecma-262/#sec-ecmascript-language-lexical-grammar:)

> There are no syntactic grammar contexts where both a leading division or division-assignment, and a leading [RegularExpressionLiteral](http://www.ecma-international.org/ecma-262/#prod-RegularExpressionLiteral) are permitted.

Notice for example that the expression `/a/ /b/g` is already permitted by the current grammar, but has a `/a/` regex literal that is divided by the variable `b` and then divided by the variable `g`.

---

<div class="post-metadata">

### Author: ![bakkot](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/bakkot/32/22_2.png) [@bakkot](https://es.discourse.group/u/bakkot)
#### Post date: [July 24, 2020, 5:00pm UTC](https://es.discourse.group/t/make-it-possible-to-divide-regular-expressions-into-chunks/355/7 "2020-07-24T17:00:51Z")

</div>

> it would just look for the next forward slash that wasn't escaped

You'd also have to exclude forward slashes within character classes: `/[/]/` is a legal regex. So it's a bit more complicated. But it's not that much more complicated; see the definition of [RegularExpressionLiteral](https://tc39.es/ecma262/#prod-RegularExpressionLiteral). (Note that there is a second, [significantly more complicated grammar](https://tc39.es/ecma262/#sec-patterns) used to parse the literals once this simpler grammar has identified them.)

---

<div class="post-metadata">

### Author: ![claudiameadows](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/claudiameadows/32/126_2.png) [@claudiameadows](https://es.discourse.group/u/claudiameadows)
#### Post date: [July 28, 2020, 9:02am UTC](https://es.discourse.group/t/make-it-possible-to-divide-regular-expressions-into-chunks/355/8 "2020-07-28T09:02:49Z")

</div>

It's a two-step process:

1. Consume a regexp token and save the inner source with its flags: [https://www.ecma-international.org/ecma-262/#sec-literals-regular-expression-literals](https://www.ecma-international.org/ecma-262/#sec-literals-regular-expression-literals).
2. Parse the inner regexp as per [https://www.ecma-international.org/ecma-262/#sec-patterns](https://www.ecma-international.org/ecma-262/#sec-patterns).

In theory, you could merge these two steps, but in practice, it's rather complicated to do (you'd have to do a fair bit of math to tame it to something actually efficiently implementable) and, unless you're writing an engine with a built-in regexp runtime (none of the major engines do, BTW) or a regexp transpiler like regexpu, it's almost never worth the effort.
