• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 191
  • Last Modified:

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?

0
spanout
Asked:
spanout
  • 2
1 Solution
 
dgofmanCommented:
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

0
 
ozoCommented:
#!/usr/bin/perl
$_='Term1 <a href="#"> term 2</a> Term1';
s#(\w+)(?!(?:(?!<a\s).)*<\/a>|[^<]*>)#<a class=glossary>$1</a>#g;
print;
0
 
spanoutAuthor Commented:
thanks for this, would this regex work in vbscript?
0
 
dgofmanCommented:
Mine will work with any scripts it´s basic RegEx
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now