obscenity / RegExpMatcher
An implementation of the [[Matcher]] interface using regular expressions and string searching methods.
• new RegExpMatcher(options
)
Creates a new [[RegExpMatcher]] with the options given.
Example
// Use the options provided by the English preset.
const matcher = new RegExpMatcher({
...englishDataset.build(),
...englishRecommendedTransformers,
});
Example
// Simple matcher that only has blacklisted patterns.
const matcher = new RegExpMatcher({
blacklistedTerms: assignIncrementingIds([
pattern`fuck`,
pattern`f?uck`, // wildcards (?)
pattern`bitch`,
pattern`b[i]tch` // optionals ([i] matches either "i" or "")
]),
});
// Check whether some string matches any of the patterns.
const doesMatch = matcher.hasMatch('fuck you bitch');
Example
// A more advanced example, with transformers and whitelisted terms.
const matcher = new RegExpMatcher({
blacklistedTerms: [
{ id: 1, pattern: pattern`penis` },
{ id: 2, pattern: pattern`fuck` },
],
whitelistedTerms: ['pen is'],
blacklistMatcherTransformers: [
resolveConfusablesTransformer(), // '🅰' => 'a'
resolveLeetSpeakTransformer(), // '$' => 's'
foldAsciiCharCaseTransformer(), // case insensitive matching
skipNonAlphabeticTransformer(), // 'f.u...c.k' => 'fuck'
collapseDuplicatesTransformer(), // 'aaaa' => 'a'
],
});
// Output all matches.
console.log(matcher.getAllMatches('fu.....uuuuCK the pen is mightier than the sword!'));
Name | Type | Description |
---|---|---|
options |
RegExpMatcherOptions |
Options to use. |
src/matcher/regexp/RegExpMatcher.ts:74
▸ getAllMatches(input
, sorted?
): MatchPayload
[]
Returns all matches of blacklisted terms in the text.
If you only need to check for the presence of a match, and do not need
more specific information about the matches, use the hasMatch()
method,
which is typically more efficient.
Name | Type | Default value | Description |
---|---|---|---|
input |
string |
undefined |
Text to find profanities in. |
sorted |
boolean |
false |
Whether the resulting list of matches should be sorted using [[compareMatchByPositionAndId]]. Defaults to false . |
A list of matches of the matcher on the text. The matches are
guaranteed to be sorted if and only if the sorted
parameter is true
,
otherwise, their order is unspecified.
src/matcher/regexp/RegExpMatcher.ts:87
▸ hasMatch(input
): boolean
Checks whether there is a match for any blacklisted term in the text.
This is typically more efficient than calling getAllMatches
and
checking the result, though it depends on the implementation.
Name | Type | Description |
---|---|---|
input |
string |
Text to check. |
boolean