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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:06:06+00:00 2026-06-12T19:06:06+00:00

I have a problem replace certain words started with # . I have the

  • 0

I have a problem replace certain words started with #. I have the following code

var x="#google",
eval("var pattern = /" + '\\b' + x + '\\b');
txt.replace(pattern,"MyNewWord");

when I use the following code it works fine

var x="google",
eval("var pattern = /" + '\\b' + x + '\\b');
txt.replace(pattern,"MyNewWord");

it works fine

any suggestion how to make the first part of code working

ps. I use eval because x will be a user input.

  • 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-12T19:06:08+00:00Added an answer on June 12, 2026 at 7:06 pm

    The problem is that \b represents a boundary between a “word” character (letter, digit, or underscore) and a “non-word” character (anything else). # is a non-word character, so \b# means “a # that is preceded by a word character” — which is not at all what you want. If anything, you want something more like \B#; \B is a non-boundary, so \B# means “a # that is not preceded by a word character”.

    I’m guessing that you want your words to be separated by whitespace, instead of by a programming-language concept of what makes something a “word” character or a “non-word” character; for that, you could write:

    var x = '#google';    // or 'google'
    var pattern = new RegExp('(^|\\s)' + x);
    var result = txt.replace(pattern, '$1' + 'MyNewWord');
    

    Edited to add: If x is really supposed to be a literal string, not a regex at all, then you should “quote” all of the special characters in it, with a backslash. You can do that by writing this:

    var x = '#google';  // or 'google' or '$google' or whatever
    var quotedX = x.replace(/[^\w\s]/g, '\\$&');
    var pattern = new RegExp('(^|\\s)' + quotedX);
    var result = txt.replace(pattern, '$1' + 'MyNewWord');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following problem. I want to delimit a string be a certain
I have a problem with the replace method of javascript. I have a string
I have a problem how do i replace a single dash(-) with a single
I have a huge problem with the REPLACE SQL function in Microsoft SQL Server
I have this stored procedure for Oracle: create or replace procedure bns_saa_message_get() <--- PROBLEM
I have a problem with Regular Expressions. I want to replace $[.....] to ${.....}.
I have a problem with an if condition that I want to use within
I'm writing a simple application where I want to replace certain words with other
I have to following code: http://www.nomorepasting.com/getpaste.php?pasteid=22987 If PHPSESSID is not already in the table
I have the following problem: I have 2 tables: 1) table1: STRANKA (ID_STRANKE(primary key),

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.