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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T14:42:40+00:00 2026-06-16T14:42:40+00:00

Using the JS replace function with regex, and will have dozens of replace statements.

  • 0

Using the JS replace function with regex, and will have dozens of replace statements.

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

To make it more readable and more manageable (i.e. easier to change the regex pattern or flags by changing it in one place instead of on every line), trying to pull the replace regex condition and replace flags out into a separate variable:

var pattern = /\b(?!\-)(?!\/)\b(?!\-)/gi;

var NewHTML = OriginalHTML
.replace("JavaScript", "<a href=\"http://js.com/\">$&</a>", pattern)
.replace("MySQL", "<a href=\"http://www.mysql.com/\">$&</a>", pattern)    

Problem is, the inline call is being completely ignored… both the regex portion and the flags portion.

Can anyone spot what’s wrong with the JS replace call or declaration of the regex/flags variable? 🙂

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-16T14:42:41+00:00Added an answer on June 16, 2026 at 2:42 pm

    I would use a lookup object to map the originals to URLs:

    // Original string
    var o = "MySql is a DBMS, whereas javascript is a client side scripting language";
    
    //Patterns
    var patterns = {
        "javascript": "http://js.com/",
        "mysql": "http://www.mysql.com/"
    };
    
    //Constructing regex
    RegExp.escape= function(s) {
        return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')
    };
    var keys = [];
    for (i in patterns) {
        if (patterns.hasOwnProperty(i)) {
            keys.push(RegExp.escape(i));
        }
    }
    var pattern = new RegExp("\\b(" + keys.join("|") + ")\\b", "gi");
    
    //Replace
    var n = o.replace(pattern, function(m, g1) {
        return "<a href='" + patterns[g1.toLowerCase()] + "'>" + g1 + "< /a>";
    });
    console.log(n);
    

    Here is a demonstration: http://jsfiddle.net/qP9Er/


    EDIT:

    As per your request, here is a version that replaces the first n occurrences. You can find a demonstration here:

    // Original string
    var o = '<p>Test 1 (JavaScript - <strong>1st keyword instance to be replaced</strong>): <br><a href="http://js1.net">Link to JavaScript site (existing URL)</a> is a scripting language commonly implemented as part of a web browser in order to create enhanced user interfaces and dynamic websites. JavaScript is very flexible.</p><p>more text here... and another mention of JavaScript. also javascript and JAVAScrIPT <br><br></p><p>Test 2 (MySQL - <strong>1st keyword instance to be replaced</strong>): <br><a href="http://www.mysql.com">MySQL</a>  (existing URL) is the most popular open-source database system.</p> <p><a href="http://www.themysqllink.com">link to a MySQL site</a> (existing URL).</p><p> More stuff about Mysql, also mysql and mySQL</p>';
    
    //Patterns
    var patterns = {
        "javascript": "http://js.com/",
        "mysql": "http://www.mysql.com/",
        "mention": "http://www.x.com/"
    };
    
    //Number of replacements
    var num = 1;
    
    //Constructing regex
    RegExp.escape = function(s) {
        return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
    };
    var keys = [];
    for (key in patterns) {
        if (patterns.hasOwnProperty(key)) {
            keys.push(RegExp.escape(key));
        }
    }
    var regexen = [];
    for (var i = 0; i < keys.length; i++) {
        regexen[i] = new RegExp("\\b(" + keys[i] + ")\\b(?![^<]*?<\/a>)", "i");
    }
    
    //Replace
    for (var i = 0; i < regexen.length; i++) {
        var count = 0;
        var pattern = regexen[i];
        while (count < num) {
            o = o.replace(pattern, function(m, g1) {
                return "<a href='" + patterns[g1.toLowerCase()] + "'>" + g1 + "</a>";
            });
            count++;
        }
    }
    document.write(o);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a regex function that will identify and replace a single
I'm using the following regex to match all words: mystr.replace(/([^\W_]+[^\s-]*) */g, function (match, p1,
I am using replace function to replace a character in the file sw.WriteLine(Regex.Replace(strLine, \\,
im new with regexp, so can i ask for some assistance Using string.replace function
I am using JavaScript's replace() function to replace a regular expression matching A-Z, a-z,
I'm using PostgreSQL 9.1.3 and the following functions: CREATE OR REPLACE FUNCTION cad(INOUT args
The function associated with the selector stops working when I replace it's contents using
i want to read an html file by using jquery get function, replace some
I am using replace method of string, var text = this ex is not
I'm using Opera 11.50 and I have a JavaScript-function that works as a callback

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.