asiniy asiniy - 4 months ago 8
Javascript Question

How to write big regexp in multiple lines javascript?

I have a long long regexp, like this (it's example):

/111|112|113|...|998|999/


How can I make it multiline to edit it with ease? Something like that:

/
111
|112
|113
...
|998
|999
/

Answer

You can create regexp using constructor:

new RegExp('/'
+ '111'
+ '|112'
+ '|113'
+ '...'
+ '|998'
+ '|999'
+ '/');

From ES6 you can also use backticks to write multiline strings:

new RegExp(`/
111
|112
|113
...
|998
|999
/`);