正则表达式用于替换 & 符号,但当它们位于 URL 中时则不替换
所以我有这个正则表达式:
&(?!#?[xX]?(?:[0-9a-fA-F]+|\w+);)
匹配文本块中的所有 &
但是,如果我有这个字符串:
& & & & & <a href="http://localhost/MyFile.aspx?mything=2&this=4">My Text &</a>
---------------------------------------------------------^
... 标记的 &也得到了目标 - 因为我用它来替换 & 与 &然后该网址就无效了:
http://localhost/MyFile.aspx?mything=2&this=4
D'oh!有谁知道更好的方法来编码不在网址中的&。
So I have this regex:
&(?!#?[xX]?(?:[0-9a-fA-F]+|\w+);)
That matches all &'s in a block of text
However, if I have this string:
& & & & & <a href="http://localhost/MyFile.aspx?mything=2&this=4">My Text &</a>
---------------------------------------------------------^
... the marked & also get's targeted - and as I'm using it to replace the &'s with & the url then becomes invalid:
http://localhost/MyFile.aspx?mything=2&this=4
D'oh! Does anyone know of a better way of encoding &'s that are not in a url.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不会,URL 不会变得无效。 HTML 代码变成:
这意味着之前没有正确编码的代码现在已经被正确编码了,链接包含的实际 URL 是:
所以, & 不是问题。代码中的字符被编码,相反,代码现在是正确的。
No, the URL does not become invalid. The HTML code becomes:
This means that the code that was not correctly encoded now is correctly encoded, and the actual URL that the link contains is:
So, it's not a problem that the & character in the code gets encoded, on the contrary the code is now correct.
在 powershell 中,可以这样做:
产生
剖析正则表达式:
In powershell this could be done as:
yields
Dissecting the regex: