[英]Javascript: How to get Substring of a string after // and before
I have a string like https://www.google.com我有一个像https://www.google.com这样的字符串
I would like to get www so the substring of a string after //
and before .
我想获得www所以在//
和之前的字符串的 substring .
(dot). (点)。
How can I do it?我该怎么做? I have tried using the split
and substring
method.我尝试使用split
和substring
方法。 But it did not work.但它没有用。
Any help would be great.任何帮助都会很棒。
Thank You.谢谢你。
Using replace()
使用replace()
let result = 'https://www.google.com'.replace(/https?:\/\/([^.]+).*/, '$1') console.log(result)
Have a look at the URL browser API if available https://developer.mozilla.org/en-US/docs/Web/API/URL查看 URL 浏览器 API 如果可用https://developer.mozilla.org/en-US/docs/Web/API/URL
Another solution would be另一种解决方案是
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"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.