我想对重复的网址进行代码检查,但是简单的字符串匹配不起作用,例如: string1 == string2
。以下面的网址为例:
答案 0 :(得分:2)
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"
答案 1 :(得分:0)
如何使用javascript正则表达式?基本上,删除http://和www。
.replace(/www.|http:\/\//g, '');
示例:
var s1 = 'www.facebook.com/authorProfile';
var s2 = 'facebook.com/authorProfile';
var s3 = 'http://www.facebook.com/authorProfile';
var s4 = 'http://facebook.com/authorProfile';
s1.replace(/www.|http:\/\//g, '');
s2.replace(/www.|http:\/\//g, '');
s3.replace(/www.|http:\/\//g, '');
s4.replace(/www.|http:\/\//g, '');
全部成为:
facebook.com/authorProfile