Replacing only exact matches in Regex

47 Views Asked by At

I have a javascript function that replaces strings that match against a profanity library with symbols. eg. damn is transformed into @#$%&!

However, this replace is non-discriminating, and will transform partial matches as well. eg. classic is transformed into cl@#$%&!ic

I'm very new to TS/JS and Regex and would like some help understanding this function's arguments and how to modify them for only exact matches.

this.replace(new RegExp(str1.replace(/([\/\,\!\\\^\$\{\}\[\]\(\)\.\*\+\?\|\<\>\-\&])/g, "\\$&"), (ignore ? "gi" : "g")), (typeof (str2) == "string") ? str2.replace(/\$/g, "$$$$") : str2);

1

There are 1 best solutions below

1
MikeM On BEST ANSWER

To avoid partial matches, the normal solution is to surround what you want to match with word boundaries \b.

The following example assumes that profanities does not contain any words that contain regex special characters.

Notice that the "shit" in "mishit" and the "ass" in "class" do not get replaced.

const profanities = ['damn', 'shit', 'ass'];

// As regex literal: /\b(?:damn|shit|ass)\b/
const re = new RegExp('\\b(?:' + profanities.join('|') + ')\\b', 'gi');

const text = "Damn, another mishit. I am shit at pool. Time to get my ass to class."; 

console.log(text.replace(re, '@#$%&!'));