I need to eliminate banned words from a text, and also remove brackets (brackets) if they had them before or after.
This is the code I tried:
window.addEventListener("load", function() {
var palabrasProhibidas = ['mala','[mala]'];
var numeroPalabrasProhibidas = palabrasProhibidas.length;
var text = "[mala]";
while(numeroPalabrasProhibidas--) {
if (text.indexOf(palabrasProhibidas[numeroPalabrasProhibidas])!=-1) {
text = text.replace(new RegExp(palabrasProhibidas[numeroPalabrasProhibidas], 'ig'), ""); // SIN PALABRAS PROHIBIDAS
}
}
var b = text.indexOf("[");
var c = text.indexOf("]");
var textc = text.replace(/[\[\]']+/g, ""); // SIN CORCHETES
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
ctx.font = "30px Arial";
ctx.fillText("sin [ ]: " + textc,5,50);
ctx.fillText("sin prohibida: " + text,5,100);
});
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>UNK</title>
</head>
<body>
<canvas id="myCanvas"></canvas>
</body>
</html>
One gives me the string without brackets and another without the forbidden words, with this my problems are:
I need to do both together. That is, remove the brackets and at the same time the forbidden words.
I can not change the variable text
. So in the end I must also draw the original word without any change (remove the brackets and the forbidden words without changing the original variable).