Search code examples
javascriptarraysreplacetagname

JavaScript lookup value from array, replace with next index


I have a string variable which is generated like this

domNodes += '<a href="javascript: void(0);" data-role="node_jump" data-node="'+this.tagName.toLowerCase()+'">'+this.tagName + "</a>" + " &raquo; ";

I also have an array which houses a 2D array with a string to lookup and a string to replace it with:

var replaceTags = [["i", "em"], ["b", "strong"]];

If this.tagName == i then replace with em the same for b and strong.

I know this is simple because I've done it before, I just can't remember how :(


Solution

  • http://jsfiddle.net/Nw45Y/

    var replaceTags = [["i", "em"], ["b", "strong"]];
    var tn = this.tagName;
    for (var i =0; i < replaceTags.length; i++) {
        tn = tn.replace(new RegExp(replaceTags[i][0], 'g'),replaceTags[i][1]);
    }