我的问题是当我尝试在我的字符串上使用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);
这段代码应该通过类名&#34; postMessage&#34;然而,用exec替换某些文本。抛出错误。类似的问题在RegExp.exec() returns NULL sporadically结束。我无法解决如何在目前的情况下做到这一点。如果有人可以帮我解决这个问题,那就太棒了。 请注意:此代码位于Google Chrome扩展脚本中!!
答案 0 :(得分:1)
请你试试如下:
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];
}
}
请让我知道