javascript - Issue With Replacing RegExp Terms In String -


var teststring = "this string has bad word in test";  function findbadwords(string) {   var badword = /\bbad\b | \bword\b | \btest\b/gi   var isbadword = string.match(badword);   if (isbadword) {     newstring = string.replace(badword," *** ");   }   document.write(newstring); } findbadwords(teststring); 

so i'm practicing regexp's , have run problem don't understand. in code above, have set regexp find "bad words" in string. can tell, have set find word "bad", "word", , "test" long there word boundary before , after word. issue i'm having "word" isn't being replaced. if put non-badword before "word" gets replaced, not otherwise. have tried taking off of word boundaries or adding non-word boundaries no luck. mind explaining why code working way , how fix it?

thanks!

also, know using document.write poor choice it's testing swear!

the issue here \b alongside " " empty space character. if remove spaces regex works well.

var teststring = "this string has bad word in test";  function findbadwords(string) {     var badword = /\bbad\b|\bword\b|\btest\b/gi     var isbadword = string.match(badword);     if (isbadword) {         newstring = string.replace(badword," *** ");     }     document.write(newstring); } findbadwords(teststring); 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -