簡體   English   中英

正則表達式匹配開頭和結尾沒有中間

[英]Regular expression match the begining and the end without the middle

我怎么能合並這兩個正則表達式? 這很有趣,因為它允許匹配字符串的開頭結尾,而不會觸及中間的內容。

function cleanURL(url) {
    url = url.replace(/^(?:https?:\/\/)?(?:www\.|ww\d\.|w\d\d\.)?/, '')
    url = url.replace(/(\/.*)/, '')
    return url
}

console.log(cleanURL('https://hello-world.example.tld/yello-blue/green')) 

結果: hello-world.example.tld

與其嘗試使用正則表達式來梳理 URL 的一部分,不如使用URL類:

new URL("https://hello-world.example.tld/yello-blue/green").hostname

這並不意味着它不能用正則表達式完成,您只需要查找///之間的任何內容。

所有這一切說,我不知道你的真實意圖是什么,因為有一幫有惡作劇的www等的URL的方式不會過濾器,它會只返回主機名。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM