1


My problem is when I try and use exec on my string, console throws this error:

Uncaught TypeError: Cannot read property '1' of null

Here's my code:

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);

This code is supposed to go through elements with the class name "postMessage" and replace certain text with the exec, however. The error is thrown. A similar problem is over at RegExp.exec() returns NULL sporadically. I can't work out how to do it with my current situation. It'd be great if someone could help me with this problem. Please note: this code is in a Google Chrome extension script!!

Billy
  • 113
  • 11

1 Answers1

1

Please could you try as in the following:

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];
       }
}

Please, let me know

gaetanoM
  • 41,594
  • 6
  • 42
  • 61