如何在JavaScript中将长正则表达式拆分为多行?

Nik*_*iko 129 javascript regex expression readability jslint

我有一个很长的正则表达式,我想在我的JavaScript代码中拆分成多行,以根据JSLint规则保持每行长度为80个字符.我认为这对阅读来说更好.这是模式样本:

var pattern = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
Run Code Online (Sandbox Code Playgroud)

Koo*_*Inc 108

您可以将其转换为字符串并通过调用new RegExp()以下方法创建表达式:

var myRE = new RegExp (['^(([^<>()[\]\\.,;:\\s@\"]+(\\.[^<>(),[\]\\.,;:\\s@\"]+)*)',
                        '|(\\".+\\"))@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.',
                        '[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\\.)+',
                        '[a-zA-Z]{2,}))$'].join(''));
Run Code Online (Sandbox Code Playgroud)

笔记:

  1. 表达式文字转换为字符串时,需要转义所有反斜杠,因为在评估字符串文字时会消耗反斜杠.(有关详细信息,请参阅Kayo的评论.)
  2. RegExp 接受修饰符作为第二个参数

    /regex/g => new RegExp('regex', 'g')

[ 加法ES20xx(标记模板)]

在ES20xx中,您可以使用标记模板.请参阅代码段.

注意:

  • 这里缺点是,你不能在正则表达式字符串使用纯空格(经常使用\s,\s+,\s{1,x},\t,\n等).

(() => {
  const createRegExp = (str, opts) => 
    new RegExp(str.raw[0].replace(/\s/gm, ""), opts || "");
  const yourRE = createRegExp`
    ^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|
    (\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|
    (([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$`;
  console.log(yourRE);
  const anotherLongRE = createRegExp`
    (\byyyy\b)|(\bm\b)|(\bd\b)|(\bh\b)|(\bmi\b)|(\bs\b)|(\bms\b)|
    (\bwd\b)|(\bmm\b)|(\bdd\b)|(\bhh\b)|(\bMI\b)|(\bS\b)|(\bMS\b)|
    (\bM\b)|(\bMM\b)|(\bdow\b)|(\bDOW\b)
    ${"gi"}`;
  console.log(anotherLongRE);
})();
Run Code Online (Sandbox Code Playgroud)

  • **注意:**使用上述答案,可以将长*正则表达式文字*分成多行.但是它需要小心,因为你不能简单地复制正则表达式文字(用`//`定义)并将它作为字符串参数粘贴到RegExp构造函数.这是因为在评估*string literal*时会消耗反斜杠字符.例如:`/ Hey\sthere /`不能被`new RegExp("Hey\sthere")替换.相反它应该被`new RegExp("Hey \\ sthere")替换.注意额外的反斜杠!因此,我更喜欢在一条长线上留下长的正则表达式字面值 (40认同)
  • 更明确的方法是创建包含有意义的子部分的命名变量,并将*那些*作为字符串或数组连接起来.这使您可以以更容易理解的方式构造`RegExp`. (5认同)
  • "新的RegExp"是多行正则表达式的绝佳方式.您可以只使用字符串连接运算符而不是连接数组:`var reg = new RegExp('^([a-'+'z] +)$','i');` (3认同)

kor*_*run 104

扩展@KooiInc答案,可以避免使用对象的source属性手动转义每个特殊字符RegExp.

例:

var urlRegex= new RegExp(''
  + /(?:(?:(https?|ftp):)?\/\/)/.source     // protocol
  + /(?:([^:\n\r]+):([^@\n\r]+)@)?/.source  // user:pass
  + /(?:(?:www\.)?([^\/\n\r]+))/.source     // domain
  + /(\/[^?\n\r]+)?/.source                 // request
  + /(\?[^#\n\r]*)?/.source                 // query
  + /(#?[^\n\r]*)?/.source                  // anchor
);
Run Code Online (Sandbox Code Playgroud)

或者如果您想避免重复该.source属性,可以使用以下Array.map()函数执行:

var urlRegex= new RegExp([
  /(?:(?:(https?|ftp):)?\/\/)/      // protocol
  ,/(?:([^:\n\r]+):([^@\n\r]+)@)?/  // user:pass
  ,/(?:(?:www\.)?([^\/\n\r]+))/     // domain
  ,/(\/[^?\n\r]+)?/                 // request
  ,/(\?[^#\n\r]*)?/                 // query
  ,/(#?[^\n\r]*)?/                  // anchor
].map(function(r) {return r.source}).join(''));
Run Code Online (Sandbox Code Playgroud)

在ES6中,map函数可以简化为: .map(r => r.source)

  • 这对于在长正则表达式中添加注释非常方便.但是,它受限于在同一行上具有匹配的括号. (8认同)
  • 这应该是公认的答案,很干净 (4认同)
  • 正是我在寻找,超级干净.谢谢! (3认同)

Ric*_*lli 24

使用字符串new RegExp很难,因为你必须转义所有的反斜杠.您可以编写较小的正则表达式并将它们连接起来.

让我们分开这个正则表达式

/^foo(.*)\bar$/
Run Code Online (Sandbox Code Playgroud)

我们将使用一个函数来使事情更美好

function multilineRegExp(regs, options) {
    return new RegExp(regs.map(
        function(reg){ return reg.source; }
    ).join(''), options);
}
Run Code Online (Sandbox Code Playgroud)

现在让我们摇滚吧

var r = multilineRegExp([
     /^foo/,  // we can add comments too
     /(.*)/,
     /\bar$/
]);
Run Code Online (Sandbox Code Playgroud)

由于它有成本,尝试只构建一次真正的正则表达式,然后使用它.


Has*_*own 9

感谢模板文字的奇妙世界,您现在可以在 ES6 中编写大的、多行的、注释良好的甚至语义嵌套的正则表达式。

//build regexes without worrying about
// - double-backslashing
// - adding whitespace for readability
// - adding in comments
let clean = (piece) => (piece
    .replace(/((^|\n)(?:[^\/\\]|\/[^*\/]|\\.)*?)\s*\/\*(?:[^*]|\*[^\/])*(\*\/|)/g, '$1')
    .replace(/((^|\n)(?:[^\/\\]|\/[^\/]|\\.)*?)\s*\/\/[^\n]*/g, '$1')
    .replace(/\n\s*/g, '')
);
window.regex = ({raw}, ...interpolations) => (
    new RegExp(interpolations.reduce(
        (regex, insert, index) => (regex + insert + clean(raw[index + 1])),
        clean(raw[0])
    ))
);
Run Code Online (Sandbox Code Playgroud)

使用它,您现在可以像这样编写正则表达式:

let re = regex`I'm a special regex{3} //with a comment!`;
Run Code Online (Sandbox Code Playgroud)

输出

/I'm a special regex{3}/
Run Code Online (Sandbox Code Playgroud)

或者多线呢?

'123hello'
    .match(regex`
        //so this is a regex

        //here I am matching some numbers
        (\d+)

        //Oh! See how I didn't need to double backslash that \d?
        ([a-z]{1,3}) /*note to self, this is group #2*/
    `)
    [2]
Run Code Online (Sandbox Code Playgroud)

输出hel,整洁!
“如果我需要实际搜索换行符怎么办?”,然后使用\n愚蠢的!
在我的 Firefox 和 Chrome 上工作。


好的,“来点更复杂的东西怎么样?”
当然,这是我正在研究的对象解构 JS 解析器的一部分

regex`^\s*
    (
        //closing the object
        (\})|

        //starting from open or comma you can...
        (?:[,{]\s*)(?:
            //have a rest operator
            (\.\.\.)
            |
            //have a property key
            (
                //a non-negative integer
                \b\d+\b
                |
                //any unencapsulated string of the following
                \b[A-Za-z$_][\w$]*\b
                |
                //a quoted string
                //this is #5!
                ("|')(?:
                    //that contains any non-escape, non-quote character
                    (?!\5|\\).
                    |
                    //or any escape sequence
                    (?:\\.)
                //finished by the quote
                )*\5
            )
            //after a property key, we can go inside
            \s*(:|)
      |
      \s*(?={)
        )
    )
    ((?:
        //after closing we expect either
        // - the parent's comma/close,
        // - or the end of the string
        \s*(?:[,}\]=]|$)
        |
        //after the rest operator we expect the close
        \s*\}
        |
        //after diving into a key we expect that object to open
        \s*[{[:]
        |
        //otherwise we saw only a key, we now expect a comma or close
        \s*[,}{]
    ).*)
$`
Run Code Online (Sandbox Code Playgroud)

它输出 /^\s*((\})|(?:[,{]\s*)(?:(\.\.\.)|(\b\d+\b|\b[A-Za-z$_][\w$]*\b|("|')(?:(?!\5|\\).|(?:\\.))*\5)\s*(:|)|\s*(?={)))((?:\s*(?:[,}\]=]|$)|\s*\}|\s*[{[:]|\s*[,}{]).*)$/

并通过一个小演示运行它?

let input = '{why, hello, there, "you   huge \\"", 17, {big,smelly}}';
for (
    let parsed;
    parsed = input.match(r);
    input = parsed[parsed.length - 1]
) console.log(parsed[1]);
Run Code Online (Sandbox Code Playgroud)

成功输出

{why
, hello
, there
, "you   huge \""
, 17
,
{big
,smelly
}
}
Run Code Online (Sandbox Code Playgroud)

请注意成功捕获带引号的字符串。
我在 Chrome 和 Firefox 上测试过,效果很好!

如果好奇,你可以检出我在做什么,以及它的示范
虽然它只适用于 Chrome,因为 Firefox 不支持反向引用或命名组。所以请注意这个答案中给出的例子实际上是一个绝育版本,可能很容易被欺骗接受无效的字符串。

  • 你应该考虑将其导出为 NodeJS 包,这太棒了 (3认同)

小智 7

这里有很好的答案,但为了完整起见,有人应该提到 Javascript 的核心特性,即使用原型链继承。像这样的事情说明了这个想法:

RegExp.prototype.append = function(re) {
  return new RegExp(this.source + re.source, this.flags);
};

let regex = /[a-z]/g
.append(/[A-Z]/)
.append(/[0-9]/);

console.log(regex); //=> /[a-z][A-Z][0-9]/g
Run Code Online (Sandbox Code Playgroud)

  • 这是这里最好的答案。 (2认同)

Anv*_*ddy 6

上面的正则表达式缺少一些无法正常工作的黑色斜线。所以,我编辑了正则表达式。请考虑这个正则表达式,它可以 99.99% 用于电子邮件验证。

let EMAIL_REGEXP = 
new RegExp (['^(([^<>()[\\]\\\.,;:\\s@\"]+(\\.[^<>()\\[\\]\\\.,;:\\s@\"]+)*)',
                    '|(".+"))@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.',
                    '[0-9]{1,3}\])|(([a-zA-Z\\-0-9]+\\.)+',
                    '[a-zA-Z]{2,}))$'].join(''));
Run Code Online (Sandbox Code Playgroud)