Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

179
Views
Chat Messages Will Not Send With Filter

Me and my friend are making a chat room for students at our school to use during free time and stuff. I am trying to add a filter so that it will remain SFW. I am very new to JavaScript so when I added the filter you couldn't send messages in the chat anymore. However when I make the filter script a comment and disable it, the messages send just fine.

Filter Code:

    var array = array.js
message.replace(array, "****");

Messaging Code:

        if (message && connected) {
  $inputMessage.val("");
  addChatMessage({
    username: username,
    message: message
  });

  socket.emit("new message", message);
}}

My friend is responsible for the Messaging code since he is much more experienced at JavaScript, but he is very busy and can't add it right now so I am trying to.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Assuming you have a array to filter which is array you could split your message by white space and then check each word to see if it contains a bad word.

Two methodologies (FYI neither is perfect. both of these solutions have plenty of edge cases/loopholes: I'd recommend a js package that has more thought put into it npm: bad-words, npm: censor-sensor, or npm: swearjar)

Both split the message message.split(' ') and then iterate over each word to see if it needs replacing .map. The first checks to see if the current word is in array and the second sees if array words contain the current word, then if so replace with ****

const array = ['badword', 'nsfw']

let message = 'my message is full of badwords which may be nsfw'

message = message.split(' ').map(word => array.includes(word.toLowerCase()) ? '*****' : word).join(' ')

console.log(message)

message = 'my message is full of badwords which may be nsfw'

message = message.split(' ').map(word => array.some(nsfword=>word.toLowerCase().includes(nsfword)) ? '*****' : word).join(' ')

console.log(message)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!