我想获得不包含特殊字符的正确视频片段标题。 我正在使用 API:
https://www.googleapis.com/youtube/v3/search
,
部分 snippet
。
目前,我正在获取下面的 snippet.title
:
I'M GONNA CARRY HER!!! Fortnite With Karina!
我期待的是这个标题:
I'm gonna carry her!!! Fortnite With Karina!
最佳答案
首先,请确认您从 API 获得的内容不是(引用您的话)特殊字符。
从技术上讲,这些字符序列是 HTML character references ,也称为 HTML entities .
您遇到的行为是 well-known issue of the API ,据我所知没有其他解决方案,除非您自己必须用这些 HTML 实体替换它们代表的实际字符。
现在,我建议不要使用临时解决方案;也就是说,我确实建议您使用编写良好、经过良好测试的知名库,这些库从符合当前 HTML 标准的精心实现的代码中派生出其重要的解决方案。
在我看来,Mathias Bynens ' 库显然是满足我上面提到的每个标准的工具:
he
he (for “HTML entities”) is a robust HTML entity encoder/decoder written in JavaScript. It supports all standardized named character references as per HTML, handles ambiguous ampersands and other edge cases just like a browser would, has an extensive test suite, and — contrary to many other JavaScript solutions — he handles astral Unicode symbols just fine. An online demo is available.
关于javascript - YouTube 视频 API 片段标题包含 Next.js 中的特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60668719/