Skip to main content
added 15 characters in body
Source Link
Leftium
  • 18k
  • 6
  • 74
  • 110
/\b(cat|dog|goatcathy|cat|catch)\b/gi

"Run code snippet" to see the results below:

var str = "I have a cat, a dogcatch, and a goatcathy.";
var mapObj = {
   catcathy:"dog""cat",
   dogcat:"goat""catch",
   goatcatch:"cat""cathy"
};
str = str.replace(/\b(cat|dog|goatcathy|cat|catch)\b/gi, function(matched){
  return mapObj[matched];
});

console.log(str);

/\b(cat|dog|goat)\b/gi

"Run code snippet" to see the results below:

var str = "I have a cat, a dog, and a goat.";
var mapObj = {
   cat:"dog",
   dog:"goat",
   goat:"cat"
};
str = str.replace(/\b(cat|dog|goat)\b/gi, function(matched){
  return mapObj[matched];
});

console.log(str);

/\b(cathy|cat|catch)\b/gi

"Run code snippet" to see the results below:

var str = "I have a cat, a catch, and a cathy.";
var mapObj = {
   cathy:"cat",
   cat:"catch",
   catch:"cathy"
};
str = str.replace(/\b(cathy|cat|catch)\b/gi, function(matched){
  return mapObj[matched];
});

console.log(str);

Source Link
Leftium
  • 18k
  • 6
  • 74
  • 110

/\b(cat|dog|goat)\b/gi

"Run code snippet" to see the results below:

var str = "I have a cat, a dog, and a goat.";
var mapObj = {
   cat:"dog",
   dog:"goat",
   goat:"cat"
};
str = str.replace(/\b(cat|dog|goat)\b/gi, function(matched){
  return mapObj[matched];
});

console.log(str);