簡體   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