-1

I'm receiving string from the back-end which is pokémon but it should be pokémon this is why I'm trying to find a way to convert é in the string to é but couldn't find any solution for that

  • My meta tag is utf-8
  • Tried normalizer
  • But do you _really_ need to decode it? Showing this text in HTML will display `é` – Justinas Jun 05 '23 at 10:20
  • I'm using react and I'm receiving string from the back end I need to send it to a deeper child component and display it I can not directly display it in the HTML this is why I'm trying to transform before sending to child component – Barış Babahan Jun 05 '23 at 10:22

1 Answers1

1

You could just use the browsers built in DOM parser. Basically set the innerHTML of a DOM element, and then read with the innerText. Your then free to use this string inside your React control.

eg.

const d = document.createElement('div');

d.innerHTML = 'pokémon';

console.log(d.innerText);

To make the above code more re-usable you could do ->

const normalizeText = (() => {
  const d = document.createElement('div');
  return t => {
    d.innerHTML = t;
    return d.innerText;
  }
})();


console.log(normalizeText('pokémon'));
Keith
  • 22,005
  • 2
  • 27
  • 44
  • Yes, that works thank you!!!. Is there any simpler way like using regex – Barış Babahan Jun 05 '23 at 10:28
  • @BarışBabahan If you was only checking for é, then you could just do a simple replace with regex, but if there lots of different ones, regex would become much more complicated, you could wrap what I've done into a simple function to make using it much easier. I'll do another snippet showing this. – Keith Jun 05 '23 at 10:31
  • 1
    I'm not sure what I can receive from back end so it's okay to create reusable function thank you – Barış Babahan Jun 05 '23 at 10:39