我想编写检查重复网址的代码,但简单的字符串匹配不起作用,例如字符串1 == 字符串2
。以以下 url 为例:
- www.facebook.com/authorProfile
- facebook.com/authorProfile
- http://www.facebook.com/authorProfile
- 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/