Line 13 should be: text = text.replace(new RegExp(`\\b${word}\\b`, "gi"), "$%#@!"); otherwise if the input is "Hell, hell, what the hello hell hello" will only get the second "hell" replaced.