文章詳情頁(yè)
正則表達(dá)式 - javascript正則列出鏈接中的字符串
瀏覽:170日期:2023-05-29 16:34:49
問題描述
http://www.xxx.com/one/two/three/four
將鏈接中每個(gè) / 后面的字符查找出來,放到一個(gè)數(shù)組里,如: [’one’,’two’,’three’,’four’] 鏈接長(zhǎng)度不限制。正則該怎么寫?
問題解答
回答1:是當(dāng)前頁(yè)面url: window.location.pathname.substr(1).split(’/’)
不是當(dāng)前頁(yè)面url:url.replace(/http(s){0,1}://[^/]+//, ’’).split(’/’)
回答2:window.location.pathname.split(’/’)
回答3:樓上的思路不錯(cuò),把前面的host去掉, 剩下的用/進(jìn)行分割,更簡(jiǎn)單一點(diǎn)
-------以下是答案
這個(gè)需要用到斷言
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. javascript - immutable配合react提升性能?2. javascript - sublime快鍵鍵問題3. javascript - nodejs關(guān)于進(jìn)程間發(fā)送句柄的一點(diǎn)疑問4. javascript - 移動(dòng)端上不能實(shí)現(xiàn)拖拽布局嗎?5. 實(shí)現(xiàn)bing搜索工具urlAPI提交6. Apache 已經(jīng)把網(wǎng)站根目錄的改為allow from all了,但是服務(wù)器還是不能訪問?7. css - 寫頁(yè)面遇到個(gè)布局問題,求大佬們幫解答,在線等,急!~8. phpstudy8.1支持win11系統(tǒng)嗎?9. 配置Apache時(shí),添加對(duì)PHP的支持時(shí)語(yǔ)法錯(cuò)誤10. vue.js - Vue 如何像Angular.js watch 一樣監(jiān)聽數(shù)據(jù)變化
排行榜

熱門標(biāo)簽
網(wǎng)公網(wǎng)安備