【问题标题】:Removing url full stop values from string "." and "。"从字符串“。”中删除 url 句号值和 ”。”
【发布时间】:2017-03-12 09:22:25
【问题描述】:
我正在编写一个 javascript 正则表达式来匹配不包含可用于 url 的特殊字符的字符串值。
我已经能够匹配 . 字符没问题,但显然 。 字符也可以作为 url 的句号,所以像 https://google。com 这样的东西进入 Chrome url 栏或作为 href value 将作为普通 url 工作。
是否还有其他字符(类似于 。)被浏览器解释为 url 分隔符?
【问题讨论】:
标签:
javascript
html
url
unicode
【解决方案1】:
是的,以下是在 URI 中被视为句号的字符:
. # "Full stop", a normal period
。# "Ideographic full stop", a Japanese period
.# "Fullwidth full stop"", a period that takes up a whole character in non-monospaced fonts
。 # "Halfwidth ideographic full stop", a Japanese period with half the width
我通过编写一个脚本来检查每个可能的字符并查看哪些字符与 URI 中的 . 行为相同来解决这个问题:
function check(char) {
try {
let u = new URL("https://example" + char + "com");
return u.hostname === "example.com";
} catch (e) {
return false
}
}
for (let i = 0; i < 65535; i++) {
let char = String.fromCharCode(i);
if (check(String.fromCharCode(i))) console.log(char, i);
}