Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 454283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:15:20+00:00 2026-05-12T22:15:20+00:00

I’ve converted html to a string, I’m able to use replace in that string

  • 0

I’ve converted html to a string, I’m able to use replace in that string to wrap the text with a link, and I can put that html back into the ID it came from.

My problem is that my replace method is going inside existing links on the page. This could create nested links, which is a problem. Does anyone out there know how to prevent the replace method from matching text that is within a link already?

I have right now:

keyword = "matching phrase";
keywordLink = "<a href='http://myurl.com'/>" + keyword + "</a>";
sasser = sasser.replace(keyword, keywordLink);
sasDom.innerHTML = sasser;

I’m looking for, in pseudo code:

... (keyword [if the next " < " sign is not followed by "/a>", regardless of how far away it is], keywordLink);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T22:15:20+00:00Added an answer on May 12, 2026 at 10:15 pm

    You can’t do this kind of thing with regex at all. Work on the document objects which are already nicely parsed into a structure for you.

    Here’s a keyword linker adapted from this question.

    // Find text in descendents of an element, in reverse document order
    // pattern must be a regexp with global flag
    //
    function findTextExceptInLinks(element, pattern, callback) {
        for (var childi= element.childNodes.length; childi-->0;) {
            var child= element.childNodes[childi];
            if (child.nodeType===1) {
                if (child.tagName.toLowerCase()!=='a')
                    findTextExceptInLinks(child, pattern, callback);
            } else if (child.nodeType===3) {
                var matches= [];
                var match;
                while (match= pattern.exec(child.data))
                    matches.push(match);
                for (var i= matches.length; i-->0;)
                    callback.call(window, child, matches[i]);
            }
        }
    }
    
    findTextExceptInLinks(document.body, /\bmatching phrase\b/g, function(node, match) {
        node.splitText(match.index+match[0].length);
        var a= document.createElement('a');
        a.href= 'http://www.example.com/myurl';
        a.appendChild(node.splitText(match.index));
        node.parentNode.insertBefore(a, node.nextSibling);
    });
    

    eta re comments: Here’s a version of the same thing using plain text matching rather than regex:

    function findPlainTextExceptInLinks(element, substring, callback) {
        for (var childi= element.childNodes.length; childi-->0;) {
            var child= element.childNodes[childi];
            if (child.nodeType===1) {
                if (child.tagName.toLowerCase()!=='a')
                    findPlainTextExceptInLinks(child, substring, callback);
            } else if (child.nodeType===3) {
                var index= child.data.length;
                while (true) {
                    index= child.data.lastIndexOf(substring, index);
                    if (index===-1)
                        break;
                    callback.call(window, child, index)
                }
            }
        }
    }
    
    var substring= 'matching phrase';
    findPlainTextExceptInLinks(document.body, substring, function(node, index) {
        node.splitText(index+substring.length);
        var a= document.createElement('a');
        a.href= 'http://www.example.com/myurl';
        a.appendChild(node.splitText(index));
        node.parentNode.insertBefore(a, node.nextSibling);
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 227k
  • Answers 227k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer % is the modulus operator. % 2 is therefore the… May 13, 2026 at 1:23 am
  • Editorial Team
    Editorial Team added an answer I read through the entire Introduction to Apple Human Interface… May 13, 2026 at 1:23 am
  • Editorial Team
    Editorial Team added an answer If you can get away with bolding the dates, just… May 13, 2026 at 1:23 am

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a French site that I want to parse, but am running into
I have text I am displaying in SIlverlight that is coming from a CMS
I want use html5's new tag to play a wav file (currently only supported

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.