2

This one seems like it has a very simple answer, yet I can't find it anywhere. I have a regular expression that is quite large, how do I put in some line breaks in the expression itself so I don't have to keep scrolling horizontally through the code to see it all?

I don't normally use word-wrap, and the IDE I'm using doesn't even offer it anyway.

Greg
  • 697
  • 1
  • 8
  • 22
  • 3
    http://stackoverflow.com/questions/12317049/how-to-split-a-long-regular-expression-into-multiple-lines-in-javascript – Loamhoof Apr 22 '13 at 14:28

2 Answers2

2

A line break in a string would normally be a \ at the end of the line :

var mystring "my string \
is on more \
than one line";

var re = new RegExp(mystring, "gim");
Tom Elmore
  • 1,980
  • 15
  • 20
1

You could use RegExp and .join() to convert and concat a string.

var myRegExp = RegExp(['/^([a-zA-Z0-9_.-])+'
              ,'@([a-zA-Z0-9_.-])+'
              ,'\.([a-zA-Z])+([a-zA-Z])+/'].join(''));

The answer has been linked to here as well.

How to split a long regular expression into multiple lines in JavaScript?

Community
  • 1
  • 1
Nery
  • 56
  • 4