只需REGEX
我只需要Javascript的regex代碼來轉換URL,例如
https://hello.romeo-juliet.fr
https://hello.romeojuliet.co.uk
https://hello.romeo-jul-iet.fr
https://hello.romeo-juliet.com
進入這個字串 romeojuliet
主要是想獲得字母域名,去除所有其他字符和https://,com/co.uk/fr等頂級域名
如果使用JS replace來完成,將會很有幫助。
我嘗試到這里
let url="https://hello.romeo-juliet.fr"/span>。
const test=url.replace(/(^w :|^)//(w 。)/, ' ')。
console.log(test);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
uj5u.com熱心網友回復:
一個非搜索引擎的解決方案:
獲取URL的主機(通過使用URL()建構式決議字串并獲取其host屬性),用句點分割并獲取所得陣列的第二項,然后洗掉所有出現的-:
。
let url="https://hello.romeo-juliet.fr"/span>。
const test = new URL(url).host。 split(".") [1]。 replaceAll("-", ''/span>)。
console.log(test);
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
uj5u.com熱心網友回復:
你可以使用它,而不需要使用以下的重碼:
你可以使用它,而不需要使用重碼。
let url="https://hello.romeo-juliet.fr"/span>。
url.substring(url.indexOf(" 。 ") 1, url.lastIndexOf(".") )。
// result: romeo-juliet
我希望這能回答你的問題
。轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/323606.html
標籤:
