Javascript 执行返回 null

Javascript exec returning null


我的问题是当我尝试在我的字符串上使用 exec 时,控制台抛出此错误:

Uncaught TypeError: Cannot read property '1' of null

这是我的代码:

var regexes = {
    "bold": [/\[bold\](.*)\[\/bold\]/g,"<b>","</b>"],
    /*"italics": [[/\[italics\]/g,"<i>"],[/\[\/italics\]/g,"</i>"]],
    "underline": [[/\[underline\]/g,"<u>"],[/\[\/underline\]/g,"</u>"]],
    "spoiler": [[/\[spoiler\]/g,"<div class='spoilerEnhanced'>"],[/\[\/spoiler\]/g,"</div>"]],
    "strikethrough": [[/\[strikethrough\]/g,"<del>"],[/\[\/strikethrough\]/g,"</del>"]],*/
}
var functioners = {
    "zalgo": [/(\[zalgo\].*\[\/zalgo\])/g],
    "flip": [/(\[flip\].*\[\/flip\])/g],
    "superscript": /[a-zA-Z0-9]\^([a-zA-Z0-9]*)/g,
    "triforce": /(%triforce)/g,
    "list": /(^\*\ .*)/g
}

var replies = document.getElementsByClassName("postMessage");
var x = 0
function memes(x,z) {
    window.setTimeout(function() {
        var theNew = "";
        theNew = replies[x].innerHTML;
        for (key in regexes) {
            if (regexes[key][0].exec(replies[x].innerHTML) !== null) {
                theNew = regexes[key][1] + regexes[key][0].exec(replies[x].innerHTML)[1] + regexes[key][2];
            }
        }
        if (x < z) {
            memes(x+1,z);
        }
    },5);
}
memes(x,replies.length-1);

此代码应该遍历具有 class 名称 "postMessage" 的元素并用 exec 替换某些文本,但是。错误被抛出。 RegExp.exec() returns NULL sporadically 也有类似的问题。我不知道如何处理我目前的情况。如果有人可以帮助我解决这个问题,那就太好了。 请注意:此代码在 Google Chrome 扩展脚本中!!

请你试试下面的方法:

for (key in regexes) {
        var m = regexes[key][0].exec(replies[x].innerHTML);
        if (m !== null) {
            theNew = regexes[key][1] + m[1] + regexes[key][2];
       }
}

请告诉我