javascript - How to replace text when the string exactly matches the search -
i using following code remove words chat text:
for(var =0; <db.banned_words.length ;i++){ cleaned_msg = cleaned_msg.replace(new regexp(db.banned_words[i], 'g'), ''); }
the problem if cleaned_msg has 1 of banned words, not remove cleaned_msg.
ex; cleaned_msg = 'duck' ; if duck 1 of banned words, then, after replace, cleaned_msg should '', 'duck'.
if cleaned_message has other spaces or characters, works fine. ex; duckie returns 'ie'; ' duck' returns ' '.
your approach should working issue lies somewhere else. may suggest different approach?
var banned_words = ['duck', 'cool', 'test'], banned_words_rx = new regexp('\\b(' + banned_words.join('|') + ')\\b', 'gi'); 'this duck cool, cool used test thing'.replace(banned_words_rx, '');
Comments
Post a Comment