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 1041359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:20:27+00:00 2026-05-16T15:20:27+00:00

I’m trying to replace all instances of a word, say foo between some HTML

  • 0

I’m trying to replace all instances of a word, say “foo” between some HTML tags.

<span id=foo> blah blah foo blah foo blah </span>

I want to replace all instances of foo that are not in the tag with bar, so the end result is:

<span id=foo> blah blah bar blah bar blah </span>

Notice that “foo” in the span tag has not been replaced.

I can manage to get the first (or last) occurance of “foo” replaced with my regular expression, but not multiple instances. Is this a situation where I should give up and not attempt to parse this with a regular expression?

Here is the regular expression that sort of works:

RegExp('(>[\\w\\s]*)\\bfoo\\b([\\w\\s]*<)',"ig"

or without javascript syntax:

s/>([\w\s]*)\bfoo\b([\w\s]*<)/

this syntax allows me to match (or should) match things like

[foo] but not bar-foo or barfoobar… any occurance of foo that will be replaced needs to stand on it’s own, it can not be contained in another word.

As a note, the “blah blah” is of varying length, and can be many different words, no words, or any combination of these.

Thanks for any suggestions.

  • 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-16T15:20:27+00:00Added an answer on May 16, 2026 at 3:20 pm

    I was attempting to do this in the wrong way. Here is the solution that I created, and seems to work great. It uses two recursive functions + DOM traversal + regular expressions to create the proper text and span nodes.

    function replaceText(element, pattern, syn_text) {
    
    for (var childi = 0; childi < element.childNodes.length;childi++) {
        var child= element2.childNodes[childi];
        if (child.nodeType==1 && child.className!=syn_text){ //make sure we don't call function on newly created node
            replaceText(child, pattern, syn_text);  //call function on child
        }
        else if (child.nodeType==3){ //this is a text node, being processing with our regular expression
            var str = child.data;
            str = str.replace(pattern,function(s, p1,p2,p3) {
                var parentNode = child.parentNode;
                do_replace(s, p1,p2,p3,parentNode,pattern,syn_text);
                parentNode.removeChild(child);  //delete old child from parent node.  we've replaced it with new nodes at this point
             });
        }
    }}
    
    
    
    
    function do_replace(s, p1,p2,p3,parentNode,pattern,syn_text) {
       if(p1.length>0){   //this might not be necessary
         //create textnode
          var text_node = document.createTextNode(p1);
          parentNode.appendChild(text_node);
       }
       if(p2.length > 0){ //create a span + next_node for the highlighting code
          spanTag = document.createElement("span");
          spanTag.id = "SString" + id++;
          spanTag.className = syn_text;
          spanTag.innerHTML = p2;
          parentNode.appendChild(spanTag);
       }
       if(p3.length > 0){
           //test to see if p3 contains another instance of our string.
    
          if(pattern.test(p3)){  //if there is a instance of our text string in the third part of the string, call function again
              p3.replace(pattern,function(s, p1,p2,p3) {
                //debugger;
                do_replace(s, p1,p2,p3,parentNode,pattern);
                return;
              });
          }
          else{  //otherwise, it's just a plain textnode, so just reinsert it.
              var text_nodep3 = document.createTextNode(p3);
              parentNode.appendChild(text_nodep3);
              return;
          }
        }
        else{ //does this do anything?
            return;
         }
    return}
    

    This function is called as follows:

    syn_highlight = "highlight_me";  //class to signify highlighting 
    pattern = new RegExp('([\\w\\W]*?)\\b('+ searchTerm + '[\\w]*)\\b([\\w\\W]*)',"ig");
    replaceText($('#BodyContent')[0],pattern,syn_highlight);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.