0

JavaScript


I have a string as follows:

#UNICODE#{1f600} #UNICODE#{1f600}

and I want to replace each occurrence of '#UNICODE#' with '\u', so the output should be like

\u{1f600} \u{1f600}

Tried many different regex in .replace function but no luck.

like

('#UNICODE#{1f600} #UNICODE#{1f600}').replace(/#UNICODE#/g,/\u/)
/\u/{1f600}/\u/{1f600}


('#UNICODE#{1f600} #UNICODE#{1f600}').replace(/#UNICODE#/g,\u)
Invalid unicode escape sequence

and so on.

Any bright ideas ? Thanks in advance.

Keval Bhogayata
  • 4,422
  • 3
  • 13
  • 36

1 Answers1

2

Escape the \\u

let str = '#UNICODE#{1f600} #UNICODE#{1f600}'.replace(/#UNICODE#/g,"\\u")

console.log(str)
str = str.replace(/\\u\{/g,"&#x").replace(/\}/g,";")
console.log(str)
document.getElementById("x").innerHTML = str
<span id="x"></span>
mplungjan
  • 169,008
  • 28
  • 173
  • 236