Commenting Regular Expressions

后端 未结 4 1303
忘掉有多难
忘掉有多难 2020-11-29 11:14

I\'m trying to comment regular expressions in JavaScript.

There seems to be many resources on how to remove comments from code using regex, but not actuall

4条回答
  •  独厮守ぢ
    2020-11-29 11:50

    Unfortunately, JavaScript doesn't have a verbose mode for regular expression literals like some other langauges do. You may find this interesting, though.

    In lieu of any external libraries, your best bet is just to use a normal string and comment that:

    var r = new RegExp(
        '('      + //start capture
        '[0-9]+' + // match digit
        ')'        //end capture
    ); 
    r.test('9'); //true
    

提交回复
热议问题