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

  • SEARCH
  • Home
  • 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 9053343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:24:01+00:00 2026-06-16T13:24:01+00:00

Trying to replace certain words in HTML pages with the same word but as

  • 0

Trying to replace certain words in HTML pages with the same word but as a URL linking to that resource.
For example, replace the word ‘MySQL’ with <a href="http://mysql.com">MySQL</a>

Using the JS replace function with regex, and it’s doing the replacing just fine.

BUT it’s also replacing words that are already part of URLs… which is the problem.

For the MySQL example, it’s replacing BOTH the “MySQL” text that’s already linked, AND the URL leading to mysql.com, so breaking the already existing link.

Is there a way to update the inline regex (in the .replace call) to NOT do replacing in existing links, i.e. elements?

Here’s the replace code:

var NewHTML = OriginalHTML
.replace(/\bJavaScript\b/gi, "<a href=\"http://js.com/\">$&</a>")
.replace(/\bMySQL\b/gi, "<a href=\"http://www.mysql.com/\">$&</a>")
;

Here’s the full sample code (tried to paste it inline but wasn’t looking right with the backticks):
http://pastie.org/private/v4l2s2c42aqduqlopurpw

Went through the JS regexp reference (here), and tried various other permutations in the regex matching, like the following, but all that does it make it not match ANY words on the page…
.replace(/\b(\<a\>*!\>)JavaScript\b/i,xxxxx

The following regex DOES prevent the match from happening wherever the word is literally touching a slash or a dash… but that’s not the solution (and it does not fix the mysql example above):

.replace(/\b(?!\>)(?!\-)(?!\/)MySQL\b(?!\-)(?!\/)/gi, "<a href=\"http://www.mysql.com/\">$&</a>")`

I’ve read through the related threads on stackoverflow and elsewhere, but can’t seem to find this particular scenario, not in JavaScript anyway.

Any help would be greatly appreciated. 🙂

Thanks!

  • 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-06-16T13:24:02+00:00Added an answer on June 16, 2026 at 1:24 pm

    You could change your regex to exclude keywords that precede the end anchor tag, </a>:

    .replace(/\bMySQL\b(?![^<]*?<\/a>)/gi, "<a href=\"http://www.mysql.com/\">$&</a>")
    

    See jsfiddle for example.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to replace a URL in CodeIgniter that has certain parameters. Not that
I am trying to replace a certain word in php and include a whitespace
I am trying to create a custom View that would replace a certain layout
I'm trying to match a certain word and replace part of the word with
I'm trying to replace certain text inside the div info when an image (which
I am trying to replace a large string in groovy. But can't get it
I have been trying to replace a word in a text file with a
I am trying to replace $1, $2, $3 variables in a URL with another
I am trying to find words and replace them using regex. I keep getting
I'm trying to remove a specific word from a certain string using the function

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.