• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 186
  • 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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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