Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

JS – Parse each word and not a string pattern

Im coding a discord.js bot to prevent from displaying message with banned words into. I have an array containing all the banned words and Im checking if the message contain one of them with .includes method.

My issue is that this method parse the whole string pattern so let’s say "apple" word is banned, if you send a message saying "Im eating a pineapple" your message will be banned because it contains "apple" somewhere in the string regardless the rest. What I want to do is check each word in isolation, not if the string contains the patern, so "Im eating an apple" should be banned for exemple and not "Im eating a pineapple".

I tried add blank spaces before and after each word in my array like " apple " but it doesn’t work cause it will say the word is fine if you dont put the spaces in the message. I had another idea which was to add in my condition something to check if character before and after the word are spaces or null like && word+1 = " " && word-1 = " " but idk if it’s doable. Here is a sample of my code :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const bannedWords = [                //banned words list
  "apple",
  "car",
  "test"
  ];

var word2parse = document.getElementById("word2parse");
var output = document.getElementById("display");

function parse(){

  if (bannedWords.some(word => word2parse.value.includes(word))){    //if message contain one  
  output.innerHTML = "this word is banned";                          //of the word in the array
  }
  else{
  output.innerHTML = "this word is fine";
  }
}
<input type = "text" id = "word2parse">
<input type = "button" onclick = "parse()" value = "Parse">
<p id = "display"></p>

>Solution :

You can use a regular expression for that with word boundaries (\b):

const bannedWords = [                //banned words list
  "apple",
  "car",
  "test"
];
const bannedRegex = RegExp("\\b(" + bannedWords.join("|") + ")\\b");

var word2parse = document.getElementById("word2parse");
var output = document.getElementById("display");

function parse() {
  if (bannedRegex.test(word2parse.value)) {
    output.innerHTML = "this text contains a banned word";
  } else{
    output.innerHTML = "this text is fine";
  }
}
<input type = "text" id = "word2parse">
<input type = "button" onclick = "parse()" value = "Parse">
<p id = "display"></p>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading