Using a filter may prevent chat messages from being sent

My friend and I are currently working on creating a chat room for students at our school to use in their free time. We wanted to make sure the chat remains safe for work, so I attempted to add a filter. However, once the filter was added, no messages could be sent in the chat. When I turned off the filter script by commenting it out, the messages were able to go through without any issues.

This is the filter code that caused the issue:

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

Below is the messaging code being used:

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

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

My friend is more experienced with JavaScript and is responsible for the Messaging code, but due to his busy schedule, I am attempting to troubleshoot the issue on my own for now.

Answer №1

To effectively filter out inappropriate words from your text, you can utilize an array called array and break down your message by spaces to examine each word for any offensive terms.

There are two approaches available (though neither is flawless as they may have certain exceptions: it is advisable to opt for a more comprehensive JavaScript library such as npm: bad-words, npm: censor-sensor, or npm: swearjar)

Both strategies involve splitting the message using message.split(' ') and then processing each word individually to determine if any replacements are needed utilizing .map. The first approach checks if the current word matches any term in the array, while the second scans through the array elements to identify if any of them are present in the current word before replacing it 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)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to trigger an Ajax function after individually selecting each checkbox in a list in MVC 4 using Razor syntax?

Is it possible to trigger an AJAX function when a checkbox within the ul below is checked? Each checkbox has a unique name attribute, such as chkbrand_1, chkbrand_2, chkbrand_3, etc. I am able to obtain the correct value using this code: $('.sear ...

Tips for adding an array to a formData: incorporating an array with two values along with their corresponding keys

Snippet : const options = new RequestOptions({ headers: headers }); let myArray; myArray = [{ "subfolder_name": subfolder, "file_upload": file }]; let formData: FormData = new FormData(); formData.append("folder_name",folder ); formData.append("counse ...

Troubleshooting problem with array of 2D textures in Threejs caused by reading raw image data

Whenever I attempt to run this example, it works perfectly fine with the provided sample. However, when I try to use my own binary file that contains a series of continuous 2D frames in JPG format which were exported from here, I end up with an image lacki ...

Incorporate Voice Recognition into an HTML document

Specifically designed for mobile devices: I am looking to implement a feature on my mobile site where a button allows users to speak into their phones. After they finish talking, a random mp3 file will play. Would it be feasible to achieve this using JQue ...

SEO Optimized pagination for pages without modifying the URL

On my website, I utilize pagination to navigate to various event pages. However, search engines are not picking up the conferences on these pages. Take a look at the code snippet below... <a href="javascript:;" class="first" title="First" onclick="getC ...

What is the correct method for formatting a JavaScript tag?

As someone who has been developing web apps since 1996, I am constantly exploring new approaches to familiar tasks. This has led me to ponder on the best JavaScript tag to use for my latest projects. Currently, I tend to utilize something like the followi ...

Struggling to fix the "TypeError: not a function" issue

Hey everyone, I've been working on setting up an Express app to connect to mongodb using the node driver for mongodb. Unfortunately, I keep encountering a TypeError: mongodbconnect is not a function. I double-checked my export/import process and made ...

Express annotations are not displaying documentation in Swagger

I recently set up a basic REST API using Express and MySQL, and now I'm looking to incorporate Swagger documentation. After watching a YouTube tutorial where annotations were used for API documentation, I implemented the api-docs route, but it seems t ...

What is the best way to make an array in jquery?

$(document).ready(function() { $("a").click(function() { const dataArray = { pageNo: $(this).text(), sortBy: $("#sortBy").val() }; $("#results").load("jquery-routing.php", dataArray ); return false; }); }); Is there a way to cr ...

Can anyone figure out why this code is not functioning properly? It seems to be targeting the body element and all of

Currently, I am utilizing jqtouch to create a mobile website. In addition to this, I am incorporating a gallery image slider into the website. However, I have encountered an issue where the images do not display when placed between <div id="project_name ...

Ways to disable an AJAX loading animation

In my current script, I am loading external content using the following code: <script type="text/javascript"> var http_request = false; function makePOSTRequest(url, parameters) { // Code for making POST request } function alertContents() { ...

How do you find a specific value in an array using a function?

As a newcomer to JavaScript and StackOverflow, I am currently working on creating a function to search for a specific value within an array I have created. Although I have written what I think is a functioning function, it seems to not be working. Any idea ...

Accessing the Parent Variable from a Function in JavaScript: A Guide

How can you properly retrieve the value of x? let x = 5 const f = (n:number) => { let x = "Welcome"; return x * n // Referring to the first x, not the second one } Also, what is the accurate technical term for this action? ...

When attempting to open a native app using JavaScript, it fails to function properly. However, the same code successfully opens the

These questions revolve around a web application developed in React that will be accessed through smartphones' browsers. I conduct testing on an iPhone using both Safari and Chrome. One of the steps involves opening a native authentication app. As pe ...

The Server Side Rendering in (Vue+Express) faltered due to inconsistencies in hydration

While browsing Vue's official website, I came across a concise example demonstrating how to implement server-side rendering (SSR) using Vue. (https://stackblitz.com/edit/vue-ssr-example-qaztqn?file=package.json) Intrigued by this example, I decided ...

The Webdriver sendKeys() function seems to be malfunctioning. Even attempting to use JavaScript as

Struggling to use WebDriver sendKeys() function to input text in a text field View the HTML below: <table class="gridtable" cellspacing="0" __gwtcellbasedwidgetimpldispatchingfocus="true" gwtcellbasedwidgetimpldispatchingblur="tru ...

Transitioning regular expressions from JavaScript to Java

I am looking for the Java equivalent of these JavaScript regex patterns: "abcde".search(/c/); // Output: 2 /[^\d]/.test("123bed567"); // Output: true "asdgh".match(/\d/); // Output: null It was a simple one-line solution in JavaScript (a ...

Various instances of controllers in Angular.js and JavaScript

I'm facing a challenge with a view that has two identical parts but different content. I want to find a way to use one controller and one view for both the left and right parts. Here's what I currently have in my HTML: <div class="board_bod ...

Exploring the distinctions between the Decorator and Mediator design patterns when implemented in JavaScript

After delving into JavaScript patterns, I noticed some interesting differences between Angular 4 and Angular 1.x. Angular 4 employs new patterns that caught my attention. What specific patterns does Angular 4 utilize? Is it possible to incorporate the De ...

JavaScript: What's the best way to update the URL in the address bar without triggering a page refresh?

Similar Question: How can I use JavaScript to update the browser URL without reloading the page? I've observed websites like GMail and GrooveShark altering the URL in the address bar without having to refresh the entire page. Based on my understa ...