A robust profanity and obscenity filtering library for JavaScript and TypeScript applications, designed to detect and censor offensive language effectively.
npm install @fevo-tech/obscenityA robust profanity and obscenity filtering library for Node.js, built on top of obscenity with extended dictionaries and HTML support.
- 🔍 Detect profanity in text strings
- 🔒 Censor bad words with customizable strategies
- 🌐 HTML-aware censoring that preserves HTML tags
- 📚 Extended dictionary including racial slurs, LGBTQ+ slurs, and other offensive terms
- ⚡ Fast and efficient using RegExp-based matching
- 🎯 TypeScript support with full type definitions
``bash`
npm install @fevo/obscenity
`typescript
import { censorBadWords, hasContainsBadWords } from "@fevo/obscenity";
// Censor bad words in a string
const text = "This is a test with some bad words";
const censored = censorBadWords(text);
console.log(censored); // "This is a test with some b** words"
// Check if text contains bad words
const hasProfanity = hasContainsBadWords(text);
console.log(hasProfanity); // true or false
`
The library can censor bad words in HTML content while preserving all HTML tags and structure:
`typescript
import { censorHtmlPreservingTags } from "@fevo/obscenity";
const html = "
This has some bad words in it
";This has some b** words in it
'API Reference
$3
Censors profanity in the input string by replacing offensive words with asterisks, keeping the first character.
Parameters:
-
input - The text string to censorReturns:
- A string with profanity censored
Example:
`typescript
censorBadWords("bad word here"); // "b** word here"
`$3
Checks whether the input string contains any profanity.
Parameters:
-
input - The text string to checkReturns:
-
true if profanity is found, false otherwiseExample:
`typescript
hasContainsBadWords("clean text"); // false
hasContainsBadWords("profane text"); // true
`$3
Censors profanity in HTML content while preserving all HTML tags, attributes, and structure.
Parameters:
-
htmlStr - The HTML string to censorReturns:
- A string with profanity censored but HTML structure intact
Example:
`typescript
censorHtmlPreservingTags("bad word");
// 'b** word'
`How It Works
The library uses:
- obscenity library for core matching and censoring functionality
- Custom extended dictionary with various categories of offensive terms
- htmlparser2 for HTML parsing and tag preservation
- RegExp-based matching with transformers for variant detection
- Keep-start censoring strategy that preserves the first character and replaces the rest with asterisks
Dictionary Coverage
The extended dictionary includes:
- Racial slurs (various ethnicities)
- LGBTQ+ slurs
- Gender-based slurs
- Ableist terms
- Sexual content
- Religious slurs
- And many other offensive terms with common variations
Development
$3
`bash
npm run build
`$3
`bash
npm run clean
``- obscenity - Core profanity detection
- htmlparser2 - HTML parsing
ISC
Contributions are welcome! Please feel free to submit a Pull Request.
This library is designed to help filter offensive content, but no automated system is perfect. The dictionary includes explicit terms for detection purposes only. Use responsibly and consider your specific use case and audience.