繁体   English   中英

Javascript:如何在 // 之后和之前获取字符串的 Substring

[英]Javascript: How to get Substring of a string after // and before

我有一个像https://www.google.com这样的字符串

我想获得www所以在//和之前的字符串的 substring . (点)。

我该怎么做? 我尝试使用splitsubstring方法。 但它没有用。

任何帮助都会很棒。

谢谢你。

使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM