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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:10:19+00:00 2026-06-02T15:10:19+00:00

I am replacing words in a string and I’m trying to match based on

  • 0

I am replacing words in a string and I’m trying to match based on '\b' + myword + '\b'
However this does not work when myword has a non word character on either end.

var myword1 = "testword,";
var myword2 = "(testword";
var testString = "<tag>asdftestword, is a (testwordasdf also (testword </tag> (testword, should be replaced everywhere there is a testword,)";

var rx1 = new RegExp(???(myword1) ,'g');
var rx2 = new RegExp(???(myword2) ,'g');
var result1 = testString.replace(rx1, "replacement");
var result2 = testString.replace(rx2, "replacement");

I would like the results to be

result1 == "<tag>asdftestword, is a (testwordasdf also (testword </tag> (replacement should be replaced everywhere there is a replacement)"
result2 == "<tag>asdftestword, is a (testwordasdf also replacement </tag> replacement, should be replaced everywhere there is a testword,)"
  • 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-02T15:10:20+00:00Added an answer on June 2, 2026 at 3:10 pm

    Ok, I’ve finally figured this out. It looks like it is too complex to do with just a single RegExp I need multiple!

    See it in action Here

    function EscapeRegex(text) {
        if (!RegExp.sRE) {
        var chars = '/.*+?|()[]{}\\'.split('');
        RegExp.sRE = new RegExp('\\'+chars.join('|\\'), 'g');
        }
        return text.replace(RegExp.sRE, '\\$&');
    }
    
    function ReplaceWholeWord(subjectString, wordtofind, replacement){
        var escapedWord = EscapeRegex(wordtofind);
        //simplest scenaro, word to find has non-word characters at begining and end - do basic replace
        if(regexIndexOf(escapedWord, '[^\\w]', 0) == 0 && regexLastIndexOf(escapedWord, '[^\\w]', 0) == wordtofind.length - 1){
            subjectString = subjectString.replace(new RegExp(escapedWord, 'g'), replacement);
        }
        //word to find begins with non-wordcharacter
        else if(regexIndexOf(escapedWord, '[^\\w]', 0) == 0){
            var index = regexIndexOf(subjectString, escapedWord+'[^\\w]', index);
            while(index > 0){
                subjectString = subjectString.substring(0, index) + replacement + subjectString.substring(index + wordtofind.length);
                index = regexIndexOf(subjectString, escapedWord+'[^\\w]', index);
            }
        }
        //word to find ends with non-wordcharacter
        else if(regexLastIndexOf(escapedWord, '[^\\w]', 0) == wordtofind.length - 1){
            var index = regexIndexOf(subjectString, '[^\\w]'+escapedWord, index);
            while(index > 0){
                subjectString = subjectString.substring(0, index) + replacement + subjectString.substring(index + myword.length + 1);
                index = regexIndexOf(subjectString, escapedWord+'[^\\w]', index);
            }
        }
        //word is normal
        else{
            var index = regexIndexOf(subjectString,'[^\\w]'+escapedWord+'[^\\w]', index);
            while(index >= 0){
                subjectString = subjectString.substring(0, index + 1) + replacement + subjectString.substring(index + wordtofind.length + 1);
                index = regexIndexOf(subjectString,'[^\\w]'+escapedWord+'[^\\w]', index);
            }
        }
        return subjectString;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I prevent javascript from replacing sections of words that match a string
I'm trying to do a Javascript replace (to remove some words from a string)
After finding the fastest string replace algorithm in this thread , I've been trying
I have this HTML string which often has a lot of whitespaces Example: <p>All
Replacing the Special Character using Perl while i am doing this . I got
While replacing a pattern in a string, I specifically need the integer/long value of
I need a systematic way of replacing each word in a string separately by
I have a string. I need to parse it, replacing any chars (except numbers
I'm trying add a space before a particular string ( Token for example) by
I am using this code for add Persian words to a csv file via

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.