Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embed comments within JavaScript regex like in Perl

Is there any way to embed a comment in a JavaScript regex, like you can do in Perl? I'm guessing there is not, but my searching didn't find anything stating you can or can't.

like image 840
xdhmoore Avatar asked Nov 08 '13 17:11

xdhmoore


People also ask

How do you comment in regex?

The (?# comment ) construct lets you include an inline comment in a regular expression. The regular expression engine does not use any part of the comment in pattern matching, although the comment is included in the string that is returned by the Regex.

How to match\\ in regex?

To match a literal backslash, you need to escape the backslash. For instance, to match the string "C:\" where "C" can be any letter, you'd use /[A-Z]:\\/ — the first backslash escapes the one after it, so the expression searches for a single literal backslash.

What is\\ d in regex?

\d (digit) matches any single digit (same as [0-9] ). The uppercase counterpart \D (non-digit) matches any single character that is not a digit (same as [^0-9] ). \s (space) matches any single whitespace (same as [ \t\n\r\f] , blank, tab, newline, carriage-return and form-feed).

How to use w in regex?

The RegExp \W Metacharacter in JavaScript is used to find the non word character i.e. characters which are not from a to z, A to Z, 0 to 9. It is same as [^a-zA-Z0-9].


1 Answers

You can't embed a comment in a regex literal.

You may insert comments in a string construction that you pass to the RegExp constructor :

var r = new RegExp(
    "\\b"   + // word boundary
    "A="    + // A=
    "(\\d+)"+ // what is captured : some digits
    "\\b"     // word boundary again
, 'i');       // case insensitive

But a regex literal is so much more convenient (notice how I had to escape the \) I'd rather separate the regex from the comments : just put some comments before your regex, not inside.

EDIT 2018: This question and answer are very old. EcmaScript now offers new ways to handle this, and more precisely template strings.

For example I now use this simple utility in node:

module.exports = function(tmpl){
    let [, source, flags] = tmpl.raw.toString()
    .replace(/\s*(\/\/.*)?$\s*/gm, "") // remove comments and spaces at both ends of lines
    .match(/^\/?(.*?)(?:\/(\w+))?$/); // extracts source and flags
    return new RegExp(source, flags);
}

which lets me do things like this or this or this:

const regex = rex`
    ^         // start of string
    [a-z]+    // some letters
    bla(\d+)
    $         // end
    /ig`;

console.log(regex); // /^[a-z]+bla(\d+)$/ig
console.log("Totobla58".match(regex)); // [ 'Totobla58' ]
like image 80
Denys Séguret Avatar answered Oct 10 '22 14:10

Denys Séguret