文章詳情頁
正則表達式 - javascript正則列出鏈接中的字符串
瀏覽:86日期: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
相關文章:
1. python文檔怎么查看?2. python - pycharm 自動刪除行尾空格3. 安全性測試 - nodejs中如何防mySQL注入4. python - pandas按照列A和列B分組,將列C求平均數,怎樣才能生成一個列A,B,C的dataframe5. python - Pycharm的Debug用不了6. html - eclipse 標簽錯誤7. python 利用subprocess庫調用mplayer時發生錯誤8. 請問PHPstudy中的數據庫如何創建索引9. datetime - Python如何獲取當前時間10. javascript - 有適合開發手機端Html5網頁小游戲的前端框架嗎?
排行榜
