Bad Words Filter

4.0.0 · active · verified Tue Apr 21

bad-words is a JavaScript library designed for filtering profanity and undesirable words from text. The current stable version is 4.0.0, which ships with TypeScript types and requires a Node.js environment version 8.0.0 or higher, or an ES2016+ compatible browser environment. While the release cadence is not strictly defined, major versions introduce significant changes, such as the removal of global string prototype modifications and the shift to modern class instantiation. Key features include a highly customizable filter that allows for placeholder overrides, advanced regex-based filtering (including multilingual support), the ability to dynamically add or remove words from the blacklist, and the option to instantiate with an empty list for specific use cases. It also incorporates Soundex support for fuzzy word comparisons, providing a robust and flexible solution for content moderation across various applications.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic instantiation, cleaning text, customizing the placeholder, and dynamically adding words to the filter list.

import { Filter } from 'bad-words';

// Create a new filter instance with default settings
const filter = new Filter();

// Clean a string, replacing bad words with asterisks
const cleanText = filter.clean("Don't be an ash0le, this is a heck of a situation.");
console.log(cleanText);
// Expected output: "Don't be an ******, this is a **** of a situation."

// Customize the placeholder character
const customFilter = new Filter({ placeHolder: 'x' });
const customCleanText = customFilter.clean("This content is pretty lame.");
console.log(customCleanText);
// Expected output: "This content is pretty xxxx."

// Add custom words to the filter's blacklist
customFilter.addWords('lame', 'heck');
const updatedCleanText = customFilter.clean("This content is pretty lame and a heck of a mess.");
console.log(updatedCleanText);
// Expected output: "This content is pretty xxxx and a xxxx of a mess."

view raw JSON →