Link to home
Start Free TrialLog in
Avatar of spanout
spanout

asked on

RegEx require to replace items when not links

we have some code that overlays a glossary of terms as jquery popups on html content. It implements this by adding <a> tag around the term to be highlighted. The problem we have is that if the term is within a hyperlink then it corrups the link. We would like to exclude any text that  is within a hypelink or other html tags. So, the replacement would take this:
Term1 <a href="#"> term 2</a>
and create this:
<a class=glossary>Term1</a> <a href="#">term 2</a>
term 2 is ignored as it is a hyperlink.

Is this possible?

Avatar of dgofman
dgofman
Flag of United States of America image

By following to your example you can use this simple pattern

  <SCRIPT LANGUAGE="JavaScript">
  <!--
	var str = 'Term1 <a href="#"> term 2</a>';
	var matches = str.match(/(\w+)(.*)/);
	if(matches.length >= 2)
		alert("<a class=glossary>" + matches[1] + "</a> " + matches[2]);
  //-->
  </SCRIPT>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of spanout
spanout

ASKER

thanks for this, would this regex work in vbscript?
Mine will work with any scripts it´s basic RegEx