URL 编码见 “&” (与号)为 “&” HTML实体
我正在对将在 URL 中传递的字符串进行编码(通过 GET)。但如果我使用 escape
、encodeURI
或 encodeURIComponent
,&
将被替换为 %26amp% 3B
,但我希望将其替换为 %26
。我做错了什么?
I am encoding a string that will be passed in a URL (via GET). But if I use escape
, encodeURI
or encodeURIComponent
, &
will be replaced with %26amp%3B
, but I want it to be replaced with %26
. What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果没有看到你的代码,除了在黑暗中刺伤之外很难回答。我猜测您传递给
encodeURIComponent()
的字符串(这是正确的使用方法)来自访问innerHTML
属性的结果。解决方案是获取 innerText/textContent 属性值:如果不是这种情况,您可以使用 replace() 方法替换 HTML 实体:
Without seeing your code, it's hard to answer other than a stab in the dark. I would guess that the string you're passing to
encodeURIComponent()
, which is the correct method to use, is coming from the result of accessing theinnerHTML
property. The solution is to get the innerText/textContent property value instead:If that isn't the case, you can usethe replace() method to replace the HTML entity:
如果你确实这样做了:
那么结果是
%26
,你可以在这里测试< /a>.确保您正在编码的字符串只是&
而不是&
开头...否则它编码正确,情况很可能是这样。如果由于某种原因需要不同的结果,可以在编码之前执行.replace(/&/g,'&')
。If you did literally this:
Then the result is
%26
, you can test it here. Make sure the string you are encoding is just&
and not&
to begin with...otherwise it is encoding correctly, which is likely the case. If you need a different result for some reason, you can do a.replace(/&/g,'&')
before the encoding.有 HTML 和 URI 编码。
&
是&
编码在 HTML 中,而%26
在 URI 编码。因此,在对字符串进行 URI 编码之前,您可能需要先进行 HTML 解码,然后再对其进行 URI 编码:)
结果
%26AndOtherHTMLEncodedStuff
希望这可以节省您一些时间
There is HTML and URI encodings.
&
is&
encoded in HTML while%26
is&
in URI encoding.So before URI encoding your string you might want to HTML decode and then URI encode it :)
result
%26AndOtherHTMLEncodedStuff
Hope this saves you some time
需要明确的是,您永远不要使用
encodeURI()
和encodeURIComponent()
。如果您不同意,请看看它的结果......这不对吧?
*
未转换!我希望您不要将其用作服务器端清理功能,因为*
不会被视为输入,而是被视为命令,即,想象一下使用rm *< 删除用户声称的文件/代码>。好吧,我希望您没有使用encodeURI() 或encodeURIComponent()!
TLDR:您实际上想要
fixedEncodeURIComponent()< /code>
和
fixedEncodeURI ()
。MDNencodeURI() 文档 ...
MDNencodeURIComponent() 文档 ...
通过这些函数,可以使用
fixedEncodeURI()
对单个 URL 片段进行编码,而fixedEncodeURIComponent()
对 URL 片段和连接器进行编码;或者,更简单地说,fixedEncodeURI()
不会对+@?=:#;,$&
进行编码(如&
和+
是常见的 URL 运算符),但fixedEncodeURIComponent()
可以。Just to be clear, you should never be using
encodeURI()
andencodeURIComponent()
. If you disagree, just look at its results...That's not right, is it?
*
did not get converted! I hope you're not using this as a server-side cleansing function, because*
will not be treated as input but as commands, i.e., imagine deleting a user's alleged file withrm *
. Well, I hope you're not using encodeURI() or encodeURIComponent()!TLDR: You actually want
fixedEncodeURIComponent()
andfixedEncodeURI()
.MDN encodeURI() Documentation...
MDN encodeURIComponent() Documentation...
With these functions, use
fixedEncodeURI()
to encode a single URL piece, whereasfixedEncodeURIComponent()
will encode URL pieces and connectors; or, more simply,fixedEncodeURI()
will not encode+@?=:#;,$&
(as&
and+
are common URL operators), butfixedEncodeURIComponent()
will.