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);
To avoid partial matches, the normal solution is to surround what you want to match with word boundaries
\b.The following example assumes that
profanitiesdoes not contain any words that contain regex special characters.Notice that the "shit" in "mishit" and the "ass" in "class" do not get replaced.