javascript - 带有 html 实体的解码字符串不等于字符串文字
问题描述
我试图实现这个线程中提出的解决方案来解码带有 html 实体的字符串,例如“foo bar”到“foo bar”。
从视觉上看,它似乎有效。但是我的快速 Jest 测试失败了:
Expected: "foobar"
Received: "foobar"
3 | describe('encryption/decodeHtml', () => {
4 | it.each([['foo bar', 'foo bar'], ['foo­bar', 'foobar'], ['foo&bar', 'foo&bar']])('should decode html entities', (val, expected) => {
> 5 | expect(decodeHtml(val)).toEqual(expected);
| ^
6 | })
7 | });
8 |
一快Object.is(decodeHtml(' '), ' ')
也产生false
。
有什么我不熟悉的 JS-Strings 吗?
解决方案
正如Andreas在评论中指出的那样,我忘记了字符串的字节表示。
看这个例子:
toBytes('foo bar') -> Uint8Array(7) [102, 111, 111, 32, 98, 97, 114]
toBytes(decodeHtml('foo bar')) -> Uint8Array(8) [102, 111, 111, 194, 160, 98, 97, 114]
事后看来,这很明显,因为中断空间和非中断空间(当然)是不同的字符。
推荐阅读
- javascript - 在 DOM 中找不到 nextElement / nextElementSibling。是因为是img吗?
- python-2.7 - 如何在 CSV 文件 python 中追加新行?
- python - 在字典列表上映射字典
- c++ - 如何在低于 7 的 Windows 版本中使用 SetDisplayConfig 功能?
- jquery - 如何在这个 jquery 函数中避免“随机”?
- sql - 将sql输出转换为以下格式
- dart - 如何在搜索代表中选择用户类型的字母?
- python - 函数列表的并行执行
- python - 如何生成 Q 函数的值
- python - 当我已经安装了 python 3.6 时,如何在运行 python(和 pipenv)2.7 的 pycharm 中设置虚拟环境?