所以,我有一个http:// adresesses的列表,我需要通过JS中的正则表达式来计算域。我不知道如何做到这一点,因为它们有不同的长度,有些相似。我怎样才能做到这一点?正则表达是我的噩梦。 here is my list
答案 0 :(得分:0)
使用此线程What is a good regular expression to match a URL?中的修改后的正则表达式,您可以计算匹配的数量,如下所示:
// Your original list of addresses
const data = `
http://www.gaba.ch/fr_CH/519/Netuschil-L-et-al-Eur-J-Oral-Sci-103-1995-355-361.htm?Subnav2=ResearchProducts&Article=17516
http://www.gaba.fi/fi_FI/725/Suche.htm?Page=42
http://www.gaba.ch/fr_CH/538/Recomend-Page.htm?LinkID=576&Brand=meridolHalitosis&Subnav=&Product=312435
http://www.gaba.com/en/1071/Professor-Edwin-G-Winkel.htm
http://www.gaba.ch/fr_CH/580/Congress-Calendar.htm?CongressId=289461&Page=6
// ... etc
`;
// Make sure you include the g flag to find all the matches and not just one
const addresses = data.match(/https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b(?:[-a-zA-Z0-9@:%_\+.~#?&//=]*)/g);
// Get length of the matched array
// - In this example: 5
// - In your case: 4815
const addressesCount = addresses.length;

修改强>
根据您的评论,我对代码进行了一些调整:
// Your original list of addresses
const data = `
http://www.gaba.ch/fr_CH/519/Netuschil-L-et-al-Eur-J-Oral-Sci-103-1995-355-361.htm?Subnav2=ResearchProducts&Article=17516
http://www.gaba.fi/fi_FI/725/Suche.htm?Page=42
http://www.gaba.ch/fr_CH/538/Recomend-Page.htm?LinkID=576&Brand=meridolHalitosis&Subnav=&Product=312435
http://www.gaba.com/en/1071/Professor-Edwin-G-Winkel.htm
http://www.gaba.ch/fr_CH/580/Congress-Calendar.htm?CongressId=289461&Page=6
// ... etc
`;
// Find all valid domains (excluding http and www)
const addresses = data.match(/https?:\/\/(?:www)?\.((?:.+?)\.[\w\.]{2,5})/g);
// Filter the addresses to only unique ones
const unique = addresses.reduce((acc, cur) => acc.indexOf(cur) > -1 ? acc : acc.concat(cur), []);
// Get number of unique addresses found
// - In this example: 3
// - In your case: 28
const length = unique.length;

注意:此http:/www.bnf.org/bnf/bnf/54/%3C
之类的地址无法匹配,因为它们无效。
答案 1 :(得分:-1)
您可以使用 String.prototype.match()方法。