文章詳情頁
正則表達(dá)式 - javascript正則列出鏈接中的字符串
瀏覽:76日期:2023-05-29 16:34:49
問題描述
http://www.xxx.com/one/two/three/four
將鏈接中每個 / 后面的字符查找出來,放到一個數(shù)組里,如: [’one’,’two’,’three’,’four’] 鏈接長度不限制。正則該怎么寫?
問題解答
回答1:是當(dāng)前頁面url: window.location.pathname.substr(1).split(’/’)
不是當(dāng)前頁面url:url.replace(/http(s){0,1}://[^/]+//, ’’).split(’/’)
回答2:window.location.pathname.split(’/’)
回答3:樓上的思路不錯,把前面的host去掉, 剩下的用/進(jìn)行分割,更簡單一點(diǎn)
-------以下是答案
這個需要用到斷言
const str = ’http://www.xxx.com/one/two/three/four’;const result = str.match(/(?/[/])w+/g).map((item) => { return item.substr(1);});// 輸出// ['www', 'one', 'two', 'three', 'four']
標(biāo)簽:
JavaScript
相關(guān)文章:
1. css3 - [CSS] 動畫效果 3D翻轉(zhuǎn)bug2. python - Django分頁和查詢參數(shù)的問題3. javascript - 百度echarts series數(shù)據(jù)更新問題4. MySQL客戶端吃掉了SQL注解?5. javascript - JS設(shè)置Video視頻對象的currentTime時出現(xiàn)了問題,IE,Edge,火狐,都可以設(shè)置,反而chrom卻...6. php自學(xué)從哪里開始?7. python小白的基礎(chǔ)問題 關(guān)于while循環(huán)的嵌套8. 求大神幫我看看是哪里寫錯了 感謝細(xì)心解答9. phpstady在win10上運(yùn)行10. javascript - 圖片能在網(wǎng)站顯示,但控制臺仍舊報錯403 (Forbidden)
排行榜

熱門標(biāo)簽