Savata Khamkar
Savata Khamkar

Reputation: 58

Replace matching word using javascript

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.

Upvotes: 0

Views: 93

Answers (4)

user123444555621
user123444555621

Reputation: 153194

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.

Upvotes: 1

erosman
erosman

Reputation: 7741

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

Upvotes: 0

RobG
RobG

Reputation: 147493

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 (.).

Upvotes: 0

jhyap
jhyap

Reputation: 3837

A noob and direct method

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

Upvotes: 0

Related Questions