Replace strings with replace JS

5

Hello I have the following code in JS that detects me in a block of content if it has any of the following sequences of characters and they have to be replaced by images, the problem is that the replace function does not allow me to replace them. Here is the code:

function validarContenido(contenido){
var emoji = [":)","XD",":P",":(",":*","X_X","|**|"];
var icono = ["img/emojis/emoji1.png",
             "img/emojis/emoji2.png",
             "img/emojis/emoji3.png",
             "img/emojis/emoji4.png",
             "img/emojis/emoji5.png",
             "img/emojis/emoji6.png",
             "img/emojis/emoji7.png"];
for(var i=0; i<emoji.length; i++){
    var estaEmoji = contenido.search(emoji[i]);
    console.log(estaEmoji);
    if(estaEmoji!=-1){
        contenido = contenido.replace(emoji[i],icono[i]);
    }
}

console.log(contenido);
return contenido;

}

And the error that throws me is the following:

 Uncaught SyntaxError: Invalid regular expression: /:)/: Unmatched ')'
 at String.search (<anonymous>)
 at validarContenido (controlador.js:195)
 at HTMLButtonElement.<anonymous> (controlador.js:151)
 at HTMLButtonElement.dispatch (jquery.min.js:2)
 at HTMLButtonElement.y.handle (jquery.min.js:2) 
    
asked by Angel Bulnes 28.04.2018 в 20:27
source

3 answers

3

Methods split() & join()

If you do not want to use Regular Expressions you can achieve what you want with the > Object Method String split() & join()

Starting from your code I leave you an example of how to do it:

function validarContenido(contenido){
  var emoji = [":)","XD",":P",":(",":*","X_X","|**|"];
  var icono = ["img/emojis/emoji1.png",
               "img/emojis/emoji2.png",
               "img/emojis/emoji3.png",
               "img/emojis/emoji4.png",
               "img/emojis/emoji5.png",
               "img/emojis/emoji6.png",
               "img/emojis/emoji7.png"];

  for(var i=0; i<emoji.length; i++) {
    contenido = contenido.split(emoji[i]).join(icono[i])
  }
  console.log(contenido);
  return contenido;
}
validarContenido(';)')
validarContenido(':)')
validarContenido(":), XD, :P, :(, :*, X_X, |**|, :), XD, :P, :(, :*, X_X, |**|");
    
answered by 30.04.2018 в 07:23
2

Are you probably looking for the indexOf method?

The search method converts the string as a parameter into a regular expression:

According to MDN about the parameter that it receives:

  

A regular expression object. If you pass an obj of object not RegExp,   it is implicitly converted to RegExp using the new RegExp (obj).

That's why I sent you the error. Try using indexOf that returns -1 if the specified string is not found:

    function validarContenido(contenido){
    var emoji = [":)","XD",":P",":(",":*","X_X","|**|"];
    var icono = ["img/emojis/emoji1.png",
                 "img/emojis/emoji2.png",
                 "img/emojis/emoji3.png",
                 "img/emojis/emoji4.png",
                 "img/emojis/emoji5.png",
                 "img/emojis/emoji6.png",
                 "img/emojis/emoji7.png"];
    for(var i=0; i<emoji.length; i++){
        var estaEmoji = contenido.indexOf(emoji[i]);
        
        if(estaEmoji!=-1){
            contenido = contenido.replace(emoji[i],icono[i]);
        }
    }

    console.log(contenido);
    return contenido;

    }

    validarContenido(':)')
    
answered by 28.04.2018 в 21:20
-1

The search function uses regular expressions , and :) is not a valid regular expression.

If you want to find an emoji as ':)' you will have to escape the special characters (in this case the parentheses):

let texto= 'esto es un emoji -> :)';

let emoji=/:\)/; // usando comillas sería ":\)"

console.log('Lo encontramos a partir de la posición',texto.search(emoji));
    
answered by 28.04.2018 в 20:35