Home > Mobile >  How to replace all emoji signs inside a string using a single javascript replace method?
How to replace all emoji signs inside a string using a single javascript replace method?

Time:10-01

I need a function that will Find smileys in text and replaces them with a replacer.

function myFunc(text, replacer) {
  return text.replace(/:(|:)|<3|:'(/g, replacer);
}

myFunc("Hi, :) I'm Sad :( :'( I can't but love <3 you", "*"));

//Output I want: Hi, * I'm Sad * * I can't but love * you

CodePudding user response:

( and ) have to be escaped in regular expressions with \

function myFunc(text, replacer) {
  return text.replace(/:\(|:\)|<3|:'\(/g, replacer);
}

const result = myFunc("Hi, :) I'm Sad :( :'( I can't but love <3 you", "*");
console.log(result)

  • Related