How to eliminate forbidden words that may or may not be surrounded by brackets

0

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).

  • asked by Eduardo Sebastian 06.06.2017 в 00:09
    source

    1 answer

    4

    It can be summarized in a single regular expression:

    /\[?(?:mala|palabra2|palabra3|etc)\]?/gi
    


    Also, you may be interested in matching words only if they are full words . For example, so that they are not removed from "comala" , nor "malaria" . For that we use the word limit \b .

    /\[?\b(?:mala|palabra2|palabra3|etc)\b\]?/gi
    


    Code

    var regex = /\[?\b(?:mala|palabra2|palabra3|etc)\b\]?/gi,
        texto = 'Elimina [mala] o mala de una frase, pero no de [mala], ni por mala, dejando malaria',
        resultado;
        
    //Reemplazar
    resultado = texto.replace(regex, '');
    
    //Mostrar el resultado
    console.log(resultado); // => Elimina  o  de una frase, pero no de , ni por , dejando malaria


    Generate the regex of dynamically banned words.

    If you also want to generate the expression from an array dynamically, you should link it with "|" , but considering that special characters have to be escaped. For that, we declare the function escaparRegex() that precedes a \ to any metacharacter.

    var texto = 'Elimina [mala] o mala de una frase, pero no de [mala], ni por mala, dejando malaria',
        resultado;
    
    
    //Listado de palabras prohibidas
    var prohibidas = ["mala", "pa^la^bra^2", "etc"];
    
    //Generar el regex
    function escaparRegex(string) {
        return string.replace(/[\^$.|?*+()[{]/g, '\$&'); 
    }
    
    var prohibidasOr = prohibidas.map(escaparRegex).join('|'),
        regex = new RegExp('\[?\b(?:' + prohibidasOr + ')\b\]?', 'gi');
    
    
    //Reemplazar
    resultado = texto.replace(regex, '');
    
    //Mostrar el resultado
    console.log(resultado); // => Elimina  o  de una frase, pero no de , ni por , dejando malaria
        
    answered by 06.06.2017 в 00:30