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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:38:44+00:00 2026-06-12T01:38:44+00:00

Since this is answered I put the simplest answer here. (Thanks to Bergi) first

  • 0

Since this is answered I put the simplest answer here. (Thanks to Bergi)

first making sure that all the special characters are escaped in ‘useWord’

useWord.replace(/([{}()[\]\\.?*+^$|=!:~-])/g, "\\$1");

then replace like so:

strIn.replace(new RexExp("\b"+userWord+"\b", "igm",replaceWord);

The original question is answered but since I need to also sometimes replace signs with words (like $ with HKD) the above mentioned answer doesn’t work:

/\$/.test("10 $ ") = true
/\b\$\b/.test("10 $ ") = false

Changed my code as there was a simple error in there:

([^A-Z]||^)

should be

([^A-Z]|^) 

Here a detailed description of the code:

The user can specify a word (=userWord variable) and the word to replace it (=replaceWord variable)

for example:

usWord=”dr”;replaceWord=”Dr.”

Replaces the word dr with Dr.

It needs to be case insensitive so DR,dR,Dr and dr will be replaced with Dr.
Problem is that the user does not know about regular expression so the ‘userWord’variable needs to be escaped: (thank you Bergi)

userWord = userWord.replace(/([{}()[\]\\.?*+^$|=!:~-])/g, "\\$1");

Now I tell JavaScript what a “word” is so “drive” would not be replaced and look like “Dr.ive” (edited: original code was wrong, had || instead of | now it’s fixed)

([^A-Z]|^) means no alphabetic character before the word or the word is at the start of the line. (“dream” and “drive” would match now but not “bandr”)

([^A-Z]|$) means no alphabetic character after the word or the word is at the end of the line. (“dream” and “drive” don’t match anymore now)

r=new RexExp("([^A-Z]|^)("+userWord+")([^A-Z]|$)","igm");

Say a variable strIn is the text that has the words that need to be replaced then now we can start replacing:

strIn=strIn.replace(r,replaceWord);

If strIn = “Julia and dr Ben.” userWord=”dr” and replaceWord=”Dr.” then the output is:

“Julia anddrBen.”

Oops, I still don’t know what the easiest way is to not have the first and last group being replaced (will read the regex tutorial tomorrow, I promise). The solution I came up with so far is:

strIn=strIn.replace(r,function(a,b,c,d){
    return b + replaceWord + d;
});

This didn’t work before because I used || instead of |

In my application the userWord and replaceWord is not one singe value but an array but in this case it doesn’t matter. Here is the full code (working) Thanks to Jim O’Brien and using $ for the different groups the code could be simplified:

userWord="dr";
replaceWord="Dr.";
strIn="Julie was driving dr David."
userWord = userWord.
    .replace(/([{}()[\]\\.?*+^$|=!:~-])/g, "\\$1");
r=new RexExp("([^A-Z]|^)("+userWord+")([^A-Z]|$)","igm");
strIn=strIn.replace(r, "$1" + replaceWord + "$2");
return strIn;
  • 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-12T01:38:45+00:00Added an answer on June 12, 2026 at 1:38 am

    Try using word boundary anchors:

    var r = new RegExp("\b"+userWord+"\b", "ig");
    return strIn.replace(r, replaceWord);
    

    Also, your regex-escaping is not really complete. Use

    userWord.replace(/([{}()[\]\\.?*+^$|=!:~-])/g, "\\$1");
    

    (the last few, after the pipe, are more or less optional depending on where you use the escaped string in a regex).

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

Sidebar

Related Questions

I'm sorry to ask this here since I'm sure it must be answered out
Previously answered questions here said that this was the fastest way: //nl is a
Require a huge help here, since this is affecting our production instance. One of
I thought that this question ( How to do attr_accessor_with_default in ruby? ) answered
I have searched this web looking for an answer, but it seems that this
I'm almost positive this has been answered already; however, unable to find it since
In pre-.NET world I always assumed that int is faster than byte since this
I have not marked this question Answered yet. The current accepted answer got accepted
Since this function is implemented in IE8, I wanted to see exactly what I
Sorry since this question is specific to my problem. While learning reflections i did

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.