下面的代码是我问题的最小表示。如何使if条件成为真,以便打印IP地址匹配!
我知道两个字符串的编码都有问题。我正在寻找一个答案,可以将两个字符串转换为传递if条件的统一编码。
var ip1="127.0.0.1";
var ip2="127․0․0․1"; //127%u20240%u20240%u20241
if(ip1 === ip2){
console.log("IP Addresses match!");
}else{
console.log("IP Addresses do not match!");
}
发布于 2017-06-30 07:53:31
基于评论中的讨论,我发布了这个答案,它在"NFKD“模式下使用了正常化功能。
var ip1="127.0.0.1";
var ip2="127․0․0․1"; //127%u20240%u20240%u20241
if(ip1.normalize("NFKD") === ip2.normalize("NFKD")){
console.log("IP Addresses match!");
}else{
console.log("IP Addresses do not match!");
}
发布于 2017-06-28 14:13:42
你可以用点代替非数字字符。
var ip1 = "127.0.0.1",
ip2 = "127․0․0․1";
ip1 = ip1.replace(/\D+/g, '.');
ip2 = ip2.replace(/\D+/g, '.');
if (ip1 === ip2){
console.log("IP Addresses match!");
} else {
console.log("IP Addresses do not match!");
}
https://stackoverflow.com/questions/44804536
复制相似问题