英文:
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('div');
d.innerHTML = 'pok&#233;mon';
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 = (() => {
const d = document.createElement('div');
return t => {
d.innerHTML = t;
return d.innerText;
}
})();
console.log(normalizeText('pok&#233;mon'));
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论