Open In App

How to handle badwords in Node.js ?

Improve
Improve
Like Article
Like
Save
Share
Report

The bad-words module is used to handle the bad-words by cleaning the given string. It is a JavaScript filter for bad words. This module searches for profane words and replaces them with placeholder characters. 

Feature of bad-words module:

  • It is easy to get started and easy to use.
  • It is a widely used and popular filter for bad words.

Installation of bad-words module:

You can visit the link to Install the bad-words module. You can install this package by using this command.

npm install bad-words

After installing the bad-words module you can check your request version in the command prompt using the command.

npm version bad-words

After that, you can create a folder and add a file, for example, index.js. To run this file you need to run the following command.

node index.js

Project Structure:

project structure

Filename: 

index.js 

javascript




const Filter = require('bad-words');
 
let filter = new Filter();
 
filter.addWords('bad', 'dumb'); // Add your own words
 
console.log(filter.clean("Hello, I am not a bad ******"));
 
let new_filter = new Filter({ placeHolder: '$' });
 
console.log(new_filter.clean("Hello, I am not a bad ******"));


Steps to run the program:

Make sure you have installed bad-words module using the following command:

npm install bad-words

Run the index.js file using the below command:

node index.js

Output of above command

So this is how you can use a bad-words module that handles bad words by cleaning the given string.


Last Updated : 03 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads