[英]Get substring before and after second space in a string via JavaScript?
[英]Javascript: How to get Substring of a string after // and before
我有一个像https://www.google.com这样的字符串
我想获得www所以在//
和之前的字符串的 substring .
(点)。
我该怎么做? 我尝试使用split
和substring
方法。 但它没有用。
任何帮助都会很棒。
谢谢你。
使用replace()
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"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.