17

From my research it looks like Javascript's regular expressions don't have any built-in equivalent to Perl's /x modifier, or .NET's RegexOptions.IgnorePatternWhitespace modifier. These are very useful as they can make a complex regex much easier to read. Firstly, have I missed something and is there a Javascript built-in equivalent to these? Secondly, if there isn't, does anyone know of a good jQuery plugin that will implement this functionality? It's a shame to have to compress my complex regex into one line because of Javascript's apparent regex limitations.

Jez
  • 27,951
  • 32
  • 136
  • 233

2 Answers2

18

If I understand you correctly you want to add white space that isn't part of the regexp? As far as I know it isn't possible with literal regexp.

Example:

var a = /^[\d]+$/

You can break up the regexp in several lines like this:

var a = RegExp(
  "^" +
  "[\\d]+" +  // This is a comment
  "$" 
);

Notice that since it is now a normal string, you have to escape \ with \\

Or if you have a complex one:

var digit_8 = "[0-9]{8}";
var alpha_4 = "[A-Za-z]{4}";
var a = RegExp(
    digit_8 +
    alpha_4 + // Optional comment
    digit_8
 );

Update: Using a temporary array to concatenate the regular expression:

var digit_8 = "[0-9]{8}";
var alpha_4 = "[A-Za-z]{4}";
var a = RegExp([
    digit_8,
    alpha_4, // Optional comment
    digit_8,
    "[0-9A-F]" // Another comment to a string
 ].join(""));
some
  • 48,070
  • 14
  • 77
  • 93
  • 2
    Yeah, but it's not the same. You've got the ugly extra Javascript string concatenation syntax, and you still can't put Perl-style comments at the end of lines, like: h(a|o)t # <-- matches hat or hot – Jez Sep 30 '10 at 13:09
  • 3
    @Jez: But you can put JavaScript comments there. There is no other way, unfortunately. JS doesn't support verbose regexes. – Tim Pietzcker Sep 30 '10 at 13:23
  • @Jez:The end result is the same, but I agree that it's uglier. You can however put comments after each line with //, or you could put the different parts of the regexp into strings with meaningful names. – some Sep 30 '10 at 13:24
  • Tim, I don't quite understand what you mean about putting JavaScript comments there. Could you post an answer giving an illustration? – Jez Sep 30 '10 at 13:25
  • @Jez: I added a comment to clarify. – some Sep 30 '10 at 13:31
  • OK some, this seems to be the best format possible given Javascript's lack of verbose regexes. Thanks. – Jez Sep 30 '10 at 15:02
16

Unfortunately there isn't such option in ES5, and I suspect it's unlikely to ever be in RegExp literals, because they're already very hard to parse and line breaks would make them even more ambiguous.

If you want easy escaping and syntax highlighting of RegExp literals, you can join them by taking advantage of the source property. It's not perfect, but IMHO less bad than falling all the way back to strings:

new RegExp(
    /foo/.source +
    /[\d+]/.source +
    /bar/.source
);

In ES6 you can create your own template string:

regexp`
  foo
  [\d+]
  bar
`;

function regexp(parts) {
   // I'm ignoring support for ${} placeholders for brevity,
   // but full implementation should escape them.

   // Note that `.raw` allows use of \d instead of \\d.
   return new RegExp(parts.raw.join('').replace(/\s+/g,''));
}
Kornel
  • 97,764
  • 37
  • 219
  • 309