文章詳情頁
正則表達(dá)式 - javascript正則列出鏈接中的字符串
瀏覽:50日期:2023-05-29 16:34:49
問題描述
http://www.xxx.com/one/two/three/four
將鏈接中每個(gè) / 后面的字符查找出來,放到一個(gè)數(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:樓上的思路不錯(cuò),把前面的host去掉, 剩下的用/進(jì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. sass - gem install compass 使用淘寶 Ruby 安裝失敗,出現(xiàn) 4042. javascript - js 對中文進(jìn)行MD5加密和python結(jié)果不一樣。3. mysql里的大表用mycat做水平拆分,是不是要先手動(dòng)分好,再配置mycat4. window下mysql中文亂碼怎么解決??5. 為啥不用HBuilder?6. python - (初學(xué)者)代碼運(yùn)行不起來,求指導(dǎo),謝謝!7. javascript - h5上的手機(jī)號默認(rèn)沒有識別8. python - 獲取到的數(shù)據(jù)生成新的mysql表9. python的文件讀寫問題?10. 為什么python中實(shí)例檢查推薦使用isinstance而不是type?
排行榜

熱門標(biāo)簽