RegExp free-spacing & comments
C. Scott Ananian
ecmascript at cscott.net
Fri Nov 6 19:24:09 UTC 2015
On Fri, Nov 6, 2015 at 1:20 PM, Brian Terlson
<Brian.Terlson at microsoft.com> wrote:
> RegExp.re or similar seems nice:
>
> ```
> let re = RegExp.re("x")`
> (\d{3}-)? # area code (optional)
> \d{3}- # prefix
> \d{4} # line number
> `;
> ```
>
> But it seems like previous proposals of this want escaping which doesn't seem ideal for this purpose. Do we need both `RegExp.re` and `RegExp.escapedRe`?
Escaping happens if you use interpolation into the string template:
```
let re = RegExp.re`(?x:
(\d{3}-)? # area code (optional)
${ /\d{3}/ }- # prefix
\d{4} # line number
( ${ "*" } \d+ )? # extension
)`;
```
If the interpolated expression is a regexp, then things seem
relatively straightforward (although there are corner cases to
consider). If the interpolated expression is a string, then it is
suggested that you use some sort of automatic escaping.
--scott
More information about the es-discuss
mailing list