如何在JavaScript中的正则表达式中包含内联注释

Pau*_*tte 8 javascript regex comments emulation inline-code

当一个字符串传递给RegExp构造函数时,内联注释有效:

RegExp("foo"/*bar*/).test("foo")
Run Code Online (Sandbox Code Playgroud)

但没有表达.JavaScript中是否有任何等效或替代方法来模拟RegExp对象的x-mode

Mar*_*der 10

Javascript既不支持x修饰符,也不支持内联注释(?#comment).看这里.

我想,你能做的最好的,是使用RegExp构造函数和写在电子独立字符串中的每个线将它们连接起来(与琴弦之间的意见):

RegExp(
    "foo" + // match a foo
    "bar" + // followed by a bar
    "$"     // at the end of the string
).test("somefoobar");
Run Code Online (Sandbox Code Playgroud)