Javascript:如何获取//和之后的字符串的子字符串

我有一个像 https://www.google.com

这样的字符串

我想获得 www ,以便在//之后和.之前的字符串的子字符串(点)。

我该怎么办? 我尝试使用splitsubstring方法。 但这没用。

任何帮助都会很棒。

谢谢。

q276941897 回答:Javascript:如何获取//和之后的字符串的子字符串

使用replace()

Regex Demo

let result = 'https://www.google.com'.replace(/https?:\/\/([^.]+).*/,'$1')

console.log(result)

,

看看URL浏览器API(如果可用)https://developer.mozilla.org/en-US/docs/Web/API/URL

另一个解决方案是

var parser = document.createElement('a');
parser.href = "http://example.com:3000/pathname/?search=test#hash";

parser.protocol; // => "http:"
parser.hostname; // => "example.com"
parser.port;     // => "3000"
parser.pathname; // => "/pathname/"
parser.search;   // => "?search=test"
parser.hash;     // => "#hash"
parser.host;     // => "example.com:3000"
本文链接:https://www.f2er.com/3168946.html

大家都在问