正規表示式 - javascript正規列出連結中的字串
世界只因有你
世界只因有你 2017-06-26 10:54:30
0
3
898

http://www.xxx.com/one/two/three/four

#將連結中每個 / 後面的字元找出來,放到一個陣列裡,如: ['one','two','three','four']
連結長度不限制。
正規該怎麼寫?

世界只因有你
世界只因有你

全部回覆(3)
Ty80

是目前頁面url: window.location.pathname.substr(1).split('/')

不是目前頁面url:url.replace(/http(s){0,1}://[^/]+//, '').split('/')

淡淡烟草味

window.location.pathname.split('/')

给我你的怀抱

樓上的思路不錯,把前面的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"]
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!