![](/img/trans.png)
[英]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.