How do I tell regexp to exclude html tags?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chainspell
    New Member
    • Aug 2009
    • 1

    How do I tell regexp to exclude html tags?

    My very first post here :) I know my way around vb and js but I'm lost with regular expressions.. Anyways I have my function below, and my problem is the function changes all words into links... even the ones inside html markups like <a href, <img, etc.

    What do I need to change on the RegExp to fix that?

    Thanks so much!

    Code:
    function linkWord(){
      var y = document.getElementById("main").getElementsByTagName("div");
      for (var i=0;i<y.length;i++){
    	var cname = y[i].getAttribute("class");
    	if (cname == "post-body") { 
    	  var x = y[i].innerHTML;
    	  var reg = new RegExp ('\\b(' + wordlist + ')\\b','gi');
    	  x = x.replace(reg, '<a href="/search/label/$1">$1</a>');
    	  y[i].innerHTML = x;
    	}
      }
    }
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    hmmm ... perhaps you could tell with plain text what you want to replace in the innerHTML of the divs? just for me ... so that i might understand the requirement a bit better :)

    kind regards

    Comment

    Working...