反应本地HTML编码

林家辉

我想在反应本机中编码链接,不知道要使用哪个库。示例链接

original 1: HSBC Holdings PLC
original 2:Software & Services
encoded 1: HSBC+Holdings+PLC
encoded 2:Software+%26+Services

我试过的

import { encode } from "html-entities";
encode("Software & Services", { mode: "nonAsciiPrintable", level: "html5" })

我得到了什么:

Software & Services
jnpdx

为了获得您要查找的转换类型(我假设这是您首先在问题中提出的示例),可以使用encodeURIComponent-这是内置于Javascript中的-无需库。默认情况下,它%20用于空格,因此您可以将使用RegEx的空格转换为加号:

encodeURIComponent("Software & Services").replace(/%20/g, "+")

您引用的HTML实体库略有不同-它转换特殊的宪章供HTML使用,而不是像您提供的示例那样的URL。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章