Censor failing to detect words containing regex symbols

I have a censor event that pulls the list of banned words, which may be preceded and/or followed by an * to mimic the Discord censor behavior. However, while I am escaping regex symbols before testing the word, my censor is failing to pick up words that contain regex symbols, and I can't for the life of me figure out why.
// Fetch all banned words from the database for the current server
const bannedWords = await Database.BannedWord.findAll({
include: [{
model: Database.Server,
where: { ServerId: message.guild.id }
}]
});

// Check if the message includes any banned word
for (const bannedWord of bannedWords) {
let word = bannedWord.Word;
let regex;
// Fetch all banned words from the database for the current server
const bannedWords = await Database.BannedWord.findAll({
include: [{
model: Database.Server,
where: { ServerId: message.guild.id }
}]
});

// Check if the message includes any banned word
for (const bannedWord of bannedWords) {
let word = bannedWord.Word;
let regex;
4 Replies
d.js toolkit
d.js toolkit7mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by OP
SowerofSystems
SowerofSystemsOP7mo ago
// Check if the word starts with 'regex:'
if (word.startsWith('regex:')) {
// Remove 'regex:' from the beginning
word = word.slice(6);
// Use the word as a regular expression
try {
regex = new RegExp(word, 'i');
} catch (error) {
console.error(`Invalid regular expression: ${word}`);
continue; // Skip this iteration and move to the next banned word
}
} else if (word.startsWith('*') && word.endsWith('*')) {
// Remove * from the beginning and end
word = word.slice(1, -1);
// Escape special characters
word = escapeRegExp(word);
// Match the word anywhere in the string
regex = new RegExp('\\b' + word + '\\b', 'i');
} else if (word.startsWith('*')) {
// Remove * from the beginning
word = word.slice(1);
// Escape special characters
word = escapeRegExp(word);
// Match the word at the end of a string
regex = new RegExp('\\b' + word + '$', 'i');
} else if (word.endsWith('*')) {
// Remove * from the end
word = word.slice(0, -1);
// Escape special characters
word = escapeRegExp(word);
// Match the word at the beginning of a string
regex = new RegExp('^' + word + '\\b', 'i');
} else {
// Escape special characters
word = escapeRegExp(word);
// Match the word as a whole word
regex = new RegExp('\\b' + word + '\\b', 'i');
}
// Check if the word starts with 'regex:'
if (word.startsWith('regex:')) {
// Remove 'regex:' from the beginning
word = word.slice(6);
// Use the word as a regular expression
try {
regex = new RegExp(word, 'i');
} catch (error) {
console.error(`Invalid regular expression: ${word}`);
continue; // Skip this iteration and move to the next banned word
}
} else if (word.startsWith('*') && word.endsWith('*')) {
// Remove * from the beginning and end
word = word.slice(1, -1);
// Escape special characters
word = escapeRegExp(word);
// Match the word anywhere in the string
regex = new RegExp('\\b' + word + '\\b', 'i');
} else if (word.startsWith('*')) {
// Remove * from the beginning
word = word.slice(1);
// Escape special characters
word = escapeRegExp(word);
// Match the word at the end of a string
regex = new RegExp('\\b' + word + '$', 'i');
} else if (word.endsWith('*')) {
// Remove * from the end
word = word.slice(0, -1);
// Escape special characters
word = escapeRegExp(word);
// Match the word at the beginning of a string
regex = new RegExp('^' + word + '\\b', 'i');
} else {
// Escape special characters
word = escapeRegExp(word);
// Match the word as a whole word
regex = new RegExp('\\b' + word + '\\b', 'i');
}
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}
AstralDev
AstralDev7mo ago
for (const bannedWord of bannedWords) {
let word = bannedWord.Word;

let escapedWord = word.replace(/[.*+?^${}()|[\]\\]/g, '\\$&').replace(/\\\*/g, '.*');


let regex = new RegExp(`\\b${escapedWord}\\b`, 'gi');

if (regex.test(message.content)) {

message.delete();
break;
}
}
for (const bannedWord of bannedWords) {
let word = bannedWord.Word;

let escapedWord = word.replace(/[.*+?^${}()|[\]\\]/g, '\\$&').replace(/\\\*/g, '.*');


let regex = new RegExp(`\\b${escapedWord}\\b`, 'gi');

if (regex.test(message.content)) {

message.delete();
break;
}
}
@SowerofSystems Try this dah one sec
for (const bannedWord of bannedWords) {
let word = bannedWord.Word;
let regex;

if (word.startsWith('regex:')) {

word = word.slice(6);


try {
regex = new RegExp(word, 'i');
} catch (error) {
console.error(`Invalid regular expression: ${word}`);
continue;
}
} else { // Handle non-regex cases
if (word.startsWith('*') && word.endsWith('*')) {
word = word.slice(1, -1);
word = escapeRegExp(word);
regex = new RegExp('\\b' + word + '\\b', 'i');
} else if (word.startsWith('*')) {
word = word.slice(1);
word = escapeRegExp(word);
regex = new RegExp('\\b' + word + '$', 'i');
} else if (word.endsWith('*')) {
word = word.slice(0, -1);
word = escapeRegExp(word);
regex = new RegExp('^' + word + '\\b', 'i');
} else {
word = escapeRegExp(word);
regex = new RegExp('\\b' + word + '\\b', 'i');
}
}

if (regex && regex.test(message.content)) {

message.delete();
break;
}
}

function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
for (const bannedWord of bannedWords) {
let word = bannedWord.Word;
let regex;

if (word.startsWith('regex:')) {

word = word.slice(6);


try {
regex = new RegExp(word, 'i');
} catch (error) {
console.error(`Invalid regular expression: ${word}`);
continue;
}
} else { // Handle non-regex cases
if (word.startsWith('*') && word.endsWith('*')) {
word = word.slice(1, -1);
word = escapeRegExp(word);
regex = new RegExp('\\b' + word + '\\b', 'i');
} else if (word.startsWith('*')) {
word = word.slice(1);
word = escapeRegExp(word);
regex = new RegExp('\\b' + word + '$', 'i');
} else if (word.endsWith('*')) {
word = word.slice(0, -1);
word = escapeRegExp(word);
regex = new RegExp('^' + word + '\\b', 'i');
} else {
word = escapeRegExp(word);
regex = new RegExp('\\b' + word + '\\b', 'i');
}
}

if (regex && regex.test(message.content)) {

message.delete();
break;
}
}

function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
This
SowerofSystems
SowerofSystemsOP7mo ago
That didn't fix it unfortunately. I is also failing to find partial words based on the *. The * were working correctly until I added the escapeRegExp, but I need it to make the censor find regex symbols as part of words, like shi+ for example. Thanks @Qjuh, that was what I was missing. I also didn't need the $ or ^ nor the \b when the word starts and ends with *.
Want results from more Discord servers?
Add your server