正则表达式无法正确匹配

时间:2015-05-04 16:53:49

标签: javascript regex string

请不要惊讶于文字的巨大墙壁。这不是一个非常专业的问题,也不需要很多阅读 问题:
我在一个数组中有一组正则表达式,它们遍历并经过一个字符串,在匹配项周围匹配和包装文本。 我有很多问题,我不知道为什么。我的大多数正则表达式与此字符串无法正确匹配:

var changelog = `+ = Added
- = Removed
~ = Changed
[line]
v1 - 4chan Enhancer is released
[line]
v2 - Minor update
Added support for new settings in the future
[line]
v3 - Major update
+ Infinite scrolling
+ Remove ads completely
+ Fetch replies automatically
+ Remove comic at top
+ Random Pepe
+ Blocked keywords
+ Changelog
+ Version
[line]
If you would like to request more features, please email me at billy@billyvenner.co.uk`;

如果我运行正则表达式,函数将返回:

<span style='color: #009<span style='color: #009110'>1</span>10'>+</span> = Added
<span style='color: #FF0000'>-</span> = Remo<span style='color: #009110'>v</span>ed
~ = Changed
[line]
v1 - 4chan Enhancer is released
[line]
v2 - Minor update
Added support for new settings in the future
[line]
v3 - Major update
+ Infinite scrolling
+ Remove ads completely
+ Fetch replies automatically
+ Remove comic at top
+ Random Pepe
+ Blocked keywords
+ Changelog
+ Version
[line]
If you would like to request more features, please email me at billy@billyvenner.co.uk
4chan Enhancer v3

使用regex101观察新的更改日志并通过它运行我的正则表达式,我的回复正是我想要匹配的内容。 由于一些奇怪的原因,所有这些只能获得一次或零次匹配。这是正则表达式所在的数组:

var formats = [
        ["^(\\+).*","<span style='color: #009110'>","</span>"],
        ["^\[line\]","<hr>","",false],
        ["^(\\~).*","<span style='color: #0086E0'>","</span>"],
        ["^(\\-).*","<span style='color: #FF0000'>","</span>"],
        ["^(v[0-9]+).*","<span style='color: #009110'>","</span>"],
        ["(http[s]{0,1}:\\/\\/www\\.[a-zA-Z0-9]*\\.[a-zA-Z0-9]*\\.[a-zA-Z0-9]*(?:\\/|-|_|=|\\?|&|[a-zA-Z0-9])*)",linkify],
        ["((?:\\/|-|_|=|\\?|&|[a-zA-Z0-9])*@(?:\\/|-|_|=|\\?|&|[a-zA-Z0-9])*\\..*\\.(?:\\/|-|_|=|\\?|&|[a-zA-Z0-9])*)",function() {linkify("mailto")}],
    ];

如果您尝试通过我的更新日志运行这些正则表达式,它可以正常工作。 请注意上面的正则表达式:你可以看到双反斜杠,因为JavaScript中的转义字符(因此它们实际上只是实际正则表达式中的一个反斜杠)而底部的两个正则表达式稍后实现,目前在我的代码。
以下是运行这些正则表达式的实际代码:

function linkify(before) {

}
function colorChangelog() {
    var newChangelog = "";
    newChangelog = changelog;
    var formats = [
        ["^(\\+).*","<span style='color: #009110'>","</span>"],
        ["^\[line\]","<hr>","",false],
        ["^(\\~).*","<span style='color: #0086E0'>","</span>"],
        ["^(\\-).*","<span style='color: #FF0000'>","</span>"],
        ["^(v[0-9]+).*","<span style='color: #009110'>","</span>"],
        ["^(http[s]{0,1}:\\/\\/www\\.[a-zA-Z0-9]*\\.[a-zA-Z0-9]*\\.[a-zA-Z0-9]*(?:\\/|-|_|=|\\?|&|[a-zA-Z0-9])*)",linkify],
        ["^((?:\\/|-|_|=|\\?|&|[a-zA-Z0-9])*@(?:\\/|-|_|=|\\?|&|[a-zA-Z0-9])*\\..*\\.(?:\\/|-|_|=|\\?|&|[a-zA-Z0-9])*)",function() {linkify("mailto")}],
    ];

    for (y = 0; y < formats.length; y++) {
        console.log(y);
        var leregex = new RegExp(formats[y][0],"g")
        var executed2 = leregex.exec(newChangelog);
        if (!!leregex.exec(newChangelog)) {
            if (!!leregex.exec(newChangelog)[1]) {
                var executed = executed2[1];
                for (match = 0; match < executed.length; match++) {
                    if (typeof(formats[y][1]) == "string") {
                        if (formats[y][formats[y].length-1] != false) {
                            var newstr = formats[y][1] + executed[match] + formats[y][2];
                            newChangelog = newChangelog.replace(executed[match],newstr);
                        } else {
                            var newstr = formats[y][1] + formats[y][2];
                            newChangelog = newChangelog.replace(executed[match],newstr);
                        }
                    } else {
                        if (typeof(formats[y][1]) == "function") {

                        } else {
                            console.log("Invalid 2nd argument: " + formats[y][1]);
                        }
                    }
                }
            }
        }
        console.log(newChangelog);
    }
    changelog = newChangelog + `
4chan Enhancer ` + version;
};
colorChangelog();
console.log(changelog);

我正在使用new RegExp运行带有"gm"标记g的正则表达式,m意味着它将尽可能匹配,而^意味着它将开始{ {1}} s和$位于的开头/结尾。
感谢您阅读这个巨大的令人生畏的文本块,希望您能提供帮助。

1 个答案:

答案 0 :(得分:0)

您的问题在于您使用exec。 .exec()将返回尚未返回的第一个匹配实例。将exec放入while循环中,你应该得到你需要的东西。你需要确保你在循环中没有任何东西会把它变成一个无限的问题。将for循环更改为以下内容似乎有效:

for (y = 0; y < formats.length; y++) {
  //console.log(y);
  var leregex = new RegExp(formats[y][0], "gm")
  while (!!(executed2 = leregex.exec(newChangelog))) {
    //console.log(executed2)
    if (!!executed2[1]) {
      if (typeof(formats[y][1]) == "string") {
        if (formats[y][formats[y].length - 1] != false) {
          var newstr = formats[y][1] + executed2[0] + formats[y][2];
          newChangelog = newChangelog.replace(executed2[0], newstr);
        } else {
          var newstr = formats[y][1] + formats[y][2];
          newChangelog = newChangelog.replace(executed2[0], newstr);
        }
      } else {
        if (typeof(formats[y][1]) == "function") {

        } else {
          console.log("Invalid 2nd argument: " + formats[y][1]);
        }
      }
    }
  }
  //console.log(newChangelog);
}

我在函数顶部实例化了execution2:

var newChangelog = "", executed2;

取消注释console.logs以查看状态滚动。

您可能希望重命名某些变量以使其更有意义(特别是已执行的变量,因为已执行不再存在

我使用developer.mozilla.org中的this reference获取有关.exec

的信息
相关问题