文章詳情頁
正則表達式 - javascript正則列出鏈接中的字符串
瀏覽:83日期:2023-05-29 16:34:49
問題描述
http://www.xxx.com/one/two/three/four
將鏈接中每個 / 后面的字符查找出來,放到一個數組里,如: [’one’,’two’,’three’,’four’] 鏈接長度不限制。正則該怎么寫?
問題解答
回答1:是當前頁面url: window.location.pathname.substr(1).split(’/’)
不是當前頁面url:url.replace(/http(s){0,1}://[^/]+//, ’’).split(’/’)
回答2:window.location.pathname.split(’/’)
回答3:樓上的思路不錯,把前面的host去掉, 剩下的用/進行分割,更簡單一點
-------以下是答案
這個需要用到斷言
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']
標簽:
JavaScript
相關文章:
排行榜
