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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:55:40+00:00 2026-05-24T16:55:40+00:00

Hi i have a Javascript function that replaces needle’s in a haystack as you

  • 0

Hi i have a Javascript function that replaces needle’s in a haystack as you will see the haystack is the entire body of the page.

function replace(needle, replacement) {
    haystackText = document.body.innerHTML
    var match = new RegExp(needle, "ig");     
    var replaced = "";
    replaced = haystackText.replace(match, replacement);
    document.body.innerHTML = replaced;
}

My problem is that the needles im looking for are not exact. For examples sake ill say the needle text is “foo”. The text i would want to replace might then look like this.

Here is some example text abcfooxyz, that is all.

I need to replace abcfooxyz, but all i know what to look for is foo. So if the replacement text was bar i need the output to look like this.

Here is some example text bar that is all.

I’m sorry if this is obvious i have searched for a solution but javascript is not my native language however im forced to use it for the implementation of this app. I can use jQuery but i would prefer not to as i have not yet called the library and i do not know it well.

Thank you for your assistance.

EDIT: Solution Found

Ok so after some additional reading and experimentation with RegExp i have written a separate function to deal with URL’s in side a a href html tag. This is used to redirect users who click outbound links to a warning page before continuing and stopping refferal and page rank data flow with rel=”noreferrer”. Here bdy is the java script documnet.body.innerHTML element.

function linkreplace(link, bdy) {
    haystackText = bdy;
    var replacement = 'a href=\"http://mysite.com/outbound_refer.php?link=' + link + '\" rel=\"noreferrer\"';
    var match = new RegExp('[^\\<\\>]*' + link + '[^\\>\\<]*', 'ig');
    replaced = haystackText.replace(match, replacement);
    return replaced;
}

Here RegExp finds the needle text and includes all text within the HTML Tag until it find the tag open and close. Here \< and \> set the expression to look for special characters of < and > so <> in regular text wont match. The ^ sets the Expression to match all characters except for the ones in the brackets.

  • 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-24T16:55:41+00:00Added an answer on May 24, 2026 at 4:55 pm

    Try this:

    var match = new RegExp('(\\b)\\w*' + needle + '\\w*(\\b)', 'ig');
    haystackText.replace(match, '$1' + replacement + '$2');
    

    Explanation of regex:

    \b        word boundary (space, punctuation, etc)
    \w*       0 or more alphanumeric characters
    [needle]  your text
    \w*       0 or more alphanumeric characters
    \b        word boundary 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a javascript function that should move a page to a certain position
I have a javascript function that manipulates the DOM when it is called (adds
I have a javascript function that calls a generic function to make an ajax
I have a javascript function that accepts a number and performs a mathematical operation
I have a javascript function that checks for a date range. Is there any
I have a javascript function that is being built to animate the collapse of
I have a javascript function that toggles the display for rows in a table.
I have a Javascript function that is called from the onchange method in a
I currently have a Javascript function that uses a string to reference an object
I have a javascript function (class) that takes a function reference as one paremter.

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.