如何将pokémon转换为重音e

huangapple go评论80阅读模式
英文:

How to convert pokémon to accent e

问题

我从后端接收到的字符串是 pokémon,但应该是 pokémon。这就是为什么我尝试找到一种方法将字符串中的 é 转换为 é,但找不到任何解决方案。

  • 我的元标签是 utf-8
  • 尝试了规范化处理
英文:

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

答案1

得分: 1

你可以直接使用浏览器内置的DOM解析器。基本上,设置一个DOM元素的innerHTML,然后使用innerText来读取。然后你可以在你的React控制中自由使用这个字符串。

例如:

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

d.innerHTML = 'pokémon';

console.log(d.innerText);

为了使上述代码更具可重用性,你可以这样做:

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

console.log(normalizeText('pokémon'));
英文:

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.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const d = document.createElement(&#39;div&#39;);

d.innerHTML = &#39;pok&amp;#233;mon&#39;;

console.log(d.innerText);

<!-- end snippet -->

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

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const normalizeText = (() =&gt; {
  const d = document.createElement(&#39;div&#39;);
  return t =&gt; {
    d.innerHTML = t;
    return d.innerText;
  }
})();


console.log(normalizeText(&#39;pok&amp;#233;mon&#39;));

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月5日 18:18:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76405450.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定