0

I need a Regex to replace only whole word.

I able to reach here.

Example

var test = "abc.com abc.com/help abc.com";
var content = "abc.com";
var replaceContent = "xyz";
var replaceRegexp = new RegExp("\\b" +content+ "\\b","g");
test.replace(replaceRegexp,replaceContent);

Getting outupt xyz xyz/help xyz

But i want output xyz abc.com/help xyz.

2
  • 2
    / is also considered to be word boundary. How exactly you want to replace? Commented May 13, 2014 at 4:46
  • Can you give a better example, you're asking to replace whole words, but the only whole word that you have there is help and the output that you want is not actually replacing 'help' Commented May 13, 2014 at 5:06

4 Answers 4

1

You don't need regular expressions for that

"abc.com abc.com/help abc.com".split(' ').reduce(function (str, word, i) {
     return str + (i ? ' ' : '') + (word == 'abc.com' ? 'xyz' : word);
}, '');

Remember that you must escape special characters like \. if you use RegExp.

Sign up to request clarification or add additional context in comments.

Comments

0

A noob and direct method

var result = test.replace(/abc.com/g,"xyz").replace("xyz/help","abc.com/help");

Comments

0

Seems you want to replace "abc.com" only when surrounded by a spaces, start and end of string:

var s = "abc.com abc.com/help abc.com";
s.replace(/(^| )abc\.com( |$)/g, '$1g$2'); // g abc.com/help g  

You can crate a function to do the above:

function replaceWholeWord(s, p, w) {
  p = p.replace(/([\\\/\.])/g,'\\$1');
  var re = new RegExp('(^| )' + p + '( |$)','g');
  return s.replace(re, '$1' + w + '$2');
}

console.log(replaceWholeWord('abc.com abc.com/help abc.com', 'abc.com/', 'g'));

However you must quote certain characters so they are treated as plain characters, not regular expression search terms (e.g. abc.com becomes abc\.com, foo/bar becomes foo\/bar, etc.). I've only included quoting of \, / and period (.).

Comments

0

If the problem is / then you can try:

var test = "abc.com abc.com/help abc.com abc.com?test abc.com";
var content = "abc\\.com";
var replaceContent = "xyz";
var replaceRegexp = new RegExp("\\b" + content + "(?!/)\\b","g");
test = test.replace(replaceRegexp,replaceContent);

console.log(test); // xyz abc.com/help xyz xyz?test xyz

If the problem is that you want the content when not followed by another string, then you can try:

var test = "abc.com something/abc.com abc.com/help abc.com abc.com?test abc.com";
var content = "abc\\.com";
var replaceContent = "xyz";
var replaceRegexp = new RegExp("\\b" + content + "(?=\\s|$)","g");
test = test.replace(replaceRegexp,replaceContent);

console.log(test); // xyz something/xyz abc.com/help xyz abc.com?test xyz

If the problem is that you want the content when not part of another string, then you can try:

var test = "abc.com something/abc.com abc.com/help abc.com abc.com?test abc.com";
var content = "abc\\.com";
var replaceContent = "xyz";
var replaceRegexp = new RegExp("(\\s|^)" + content + "(?=\\s|$)","g");
test = test.replace(replaceRegexp, '$1' + replaceContent);

console.log(test); // xyz something/abc.com abc.com/help xyz abc.com?test xyz

Update: Finally, as mentioned by RobG, the . in content will have to be escaped

If there will only be a ., then a simple content.replace('.', '\\.') will do the job ..or content.replace(/\./g, '\\$&') if there will be more than one

For a more comprehensive approach, try this:

var test = "abc.com abcdcom something/abc.com abc.com/help abc.com abc.com?test abc.com";
var content = "abc.com";
var replaceContent = "xyz";
var rQuantifiers = /[-\/\\^$*+?.()|[\]{}]/g;
var replaceRegexp = new RegExp("(\\s|^)" + content.replace(rQuantifiers, '\\$&') + "(?=\\s|$)","g");
test = test.replace(replaceRegexp, '$1' + replaceContent);

console.log(test); // xyz abcdcom something/abc.com abc.com/help xyz abc.com?test xyz

1 Comment

Note that \b matches hyphen (-) also, and that simply putting content into a regular expression constructor will treat many characters as special, such as \, /, ., ? and others. So "abc.com" will match "abcXcom", i.e. "abc <any character here> com".

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.