<a>
DOM元素爲您提供了這種拆分hrefs!這裏是如何:
var a = document.createElement('a');
a.href = 'https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=picture%20of%20a%20potato';
console.log({
protocol: a.protocol,
host: a.host,
hostname: a.hostname,
port: a.port,
pathname: a.pathname,
search: a.search
});
回報:
{
"protocol": "https:",
"host": "www.google.com",
"hostname": "www.google.com",
"port": "",
"pathname": "/webhp",
"search": "?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8"
}
更多信息請參見https://www.abeautifulsite.net/parsing-urls-in-javascript。
當你調用'.match()'它返回一個包含了比賽,所有的拍攝組的數組。順便說一句,在整個正則表達式中放置一個捕獲組是毫無意義的。 – Barmar
https://www.abeautifulsite.net/parsing-urls-in-javascript –
['/(https?:。*?\。{1,3})/ gi'不會捕獲https:// www .google.com'](https://regex101.com/r/CEntTi/2) –