RegEx require to replace items when not links

Posted on 2011-10-21
Last Modified: 2012-05-12
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?

Question by:spanout
    LVL 29

    Expert Comment

    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]);

    Open in new window

    LVL 84

    Accepted Solution

    $_='Term1 <a href="#"> term 2</a> Term1';
    s#(\w+)(?!(?:(?!<a\s).)*<\/a>|[^<]*>)#<a class=glossary>$1</a>#g;

    Author Comment

    thanks for this, would this regex work in vbscript?
    LVL 29

    Expert Comment

    Mine will work with any scripts it´s basic RegEx

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Introduction:   Welcome to my first article ever. To begin with, the reason I write this article.  I participated in a question on Experts Exchange about the start command in Windows and there were some discussion about the usage. The discussio…
    Making a simple AJAX shopping cart Couple years ago I made my first shopping cart, I used iframe and JavaScript, it was very good at that time, there were no sessions or AJAX, I used cookies on clients machine. Today we have more advanced techno…
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now