Full word To differentiate fuck
from brainfuck
, we use \b
, which match the full word limits ( or word boundaries .
/\bfuck\b/i
Repeated Characters . To match any number of repeated characters, we use the quantifier < a href="https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions#special-plus"> +
, which repeats the previous structure 1 or more times. Thus, /f+/
matches 1 to infinite "f"
, or /f+u+/
could match "fffffffffuu"
. More info on Repetition .
/\bf+u+c+k+\b/i
Intermediate spaces . To allow any number of spaces between the letters, we use a repeated space with a asterisk (0 or more times).
/\bf+ *u+ *c+ *k+\b/i
Other intermediate characters . For the solution you're looking for, instead of spaces, I think you should allow any non-alphanumeric character. \W
matches characters that are not " catacteres de palabras ", that is, any character except [a-zA-Z0-9_]
. In this way, it would match texts as "(f)(u)(c)(k)"
. More info at Shorthands .
/\bf+\W*u+\W*c+\W*k+\b/i
Demo:
let pruebas = [
"prueba",
"palabra fuck bloqueada",
"palabra brainfuck está bien",
"con espacios f u c k",
"caracteres repetidos fuuuucccckkk!!",
"con símbolos F::u--C**K!!!"
],
regex = /\bf+\W*u+\W*c+\W*k+\b/i;
for (let string of pruebas) {
console.log('"${string}" -->', regex.test(string));
}
Multiple words . In addition, more than one word can be included within the same regular expression, grouping with (?:
expresión1
|
expresión2
)
. For example, to match fuck
or ban
:
/\b(?:f+\W*u+\W*c+\W*k+|b+\W*a+\W*n+)\b/i
If you had an extremely long list, since I do not know the limits for number of characters or compiled regex (or how it would affect efficiency), you should try before implementing if you intend to use it with a lot of words.
Generate the expression by code . An essential point in this solution is to generate the regex dynamically. The following function takes an array of forbidden words and returns a RegExp object with the pattern of this response.
function regexDePalabrasProhibidas(arrListado) {
let exprProhibida = arrListado.reduce(function(acum, item, index) {
//unir las palabras con "|"
return acum + (index ? "|" : "") +
item.replace(/\w(?=(\w)?)/g, function(letra, tieneSiguiente) {
//agregar "\W*" entre caracteres
return letra + "+" + (tieneSiguiente ? "\W*" : "");
});
}, "");
//regex con límites de palabra y agrupado
return new RegExp("\b(?:" + exprProhibida + ")\b", "i");
}
// --- EJEMPLO ---
let listado = [
"fuck",
"ban",
"palabra3",
"palabra4"
],
regex,
pruebas = [
"prueba",
"palabra fuck bloqueada",
"palabra brainfuck está bien",
"con espacios f u c k",
"caracteres repetidos fuuuucccckkk!!",
"con símbolos F::u--C**K!!!",
"frase con (b)(a)(n)"
];
regex = regexDePalabrasProhibidas(listado);
document.body.innerHTML = 'Regex final: <code>/${regex.source}/${regex.flags}</code>';
for (let string of pruebas) {
console.log('"${string}" -->', regex.test(string));
}