文章詳情頁
正則表達(dá)式 - javascript正則列出鏈接中的字符串
瀏覽:79日期: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)前頁面url: window.location.pathname.substr(1).split(’/’)
不是當(dāng)前頁面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. objective-c - ios百度地圖定位問題2. html5 - 如何解決bootstrap打開模態(tài)modal窗口引起頁面抖動(dòng)?3. javascript - 求助關(guān)于js正則問題4. javascript - node.js服務(wù)端渲染解疑5. javascript - 求助這種功能有什么好點(diǎn)的插件?6. html5 - rudy編譯sass的時(shí)候有中文報(bào)錯(cuò)7. html - css 如何添加這種邊框?8. 為何 localStorage、sessionStorage 屬于html5的范疇,但是為何 IE8卻支持?9. 微信開放平臺(tái) - Android調(diào)用微信分享不顯示10. javascript - 關(guān)于定時(shí)器 與 防止連續(xù)點(diǎn)擊 問題
排行榜

熱門標(biāo)簽