javascript - 使用 javascript 检测域名的重复 URL

标签 javascript url dns

我想编写检查重复网址的代码,但简单的字符串匹配不起作用,例如字符串1 == 字符串2。以以下 url 为例:

  1. www.facebook.com/authorProfile
  2. facebook.com/authorProfile
  3. http://www.facebook.com/authorProfile
  4. http://facebook.com/authorProfile

最佳答案

function extract(str){
    var patterns = [
        "http://www.",
        "http://",
        "www."
    ];
    for(var i=0, len=patterns.length; i < len; i++){
        var pattern = patterns[i];
        if(str.indexOf(pattern) == 0)
            return str.substring(pattern.length);
    }
    return str;
}

这会将所有这些链接转换为 facebook.com/authorProfile 样式,以便您可以比较它们。

links = [
    "www.facebook.com/authorProfile",
    "facebook.com/authorProfile",
    "http://www.facebook.com/authorProfile",
    "http://facebook.com/authorProfile"
];

for(var i=0, len=links.length; i<len; i++){
    console.log( extract(links[i]) );
}
// will produce 4 "facebook.com/authorProfile"

关于javascript - 使用 javascript 检测域名的重复 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10780284/

相关文章:

javascript - 图片最大高度

CodeIgniter base_url(),本地主机中的链接为我提供了实时服务器

c# - 在标签上显示首选 DNS 和备用 DNS

JavaScript print() 不带 URL 和日期

javascript - Modal仅在ajax请求后触发

javascript - Vanilla JS Div 碰撞检测

ios - 在 TableView 单元格选择上打开 URL(RSS 阅读器)

android - 图像显示为gridview

tomcat - liferay 6.1 和 Tomcat 7 - 更改端口

dns - BIND 9.9 显示的 ADDITIONAL SECTION 数量是否错误?