2

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.

2
  • 4
    Don't eval() these! Use new RegExp('\\b' + x + '\\b') Commented Oct 13, 2012 at 2:16
  • 5
    "because x will be a user input" is a reason not to use eval Commented Oct 13, 2012 at 2:16

4 Answers 4

1

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');
Sign up to request clarification or add additional context in comments.

2 Comments

Hello Ruakh your code works perfect if x="#google" or x="?google" but if x = "$google" it doesnt work have you got any idea ?
@NavinHarishK: That's because $ has a special meaning in regular expressions -- it means "end of string" -- so the regex /(^|\s)$google/ is never matched by anything. To match an actual dollar sign, you'd have to "quote" it with a backslash: /(^|\s)\$google/. If you're expecting x to be a literal string, and you want to find occurrences of exactly it, then you'd want to start by quoting all of its special characters. Here, I'll update my answer . . .
1

Make you patter something like this:

/(#)?\w*/

1 Comment

and where to put my variable x ?
1

If you want to make a Regular Expression, try this instead of eval:

var pattern = new RegExp(x);

Btw the line:

eval("var pattern = /" + '\\b' + x + '\\b');

will make an error because of no enclose pattern, should be :

eval("var pattern = /" + '\\b' + x + '\\b/');

Comments

0

How about

var x = "#google";

x.match(/^\#/);

1 Comment

I need to replace every google (WORD) starts with # for example I don't want to replace google norgoogle I want to replace only #google

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.