首頁  >  問答  >  主體

正規表示式 - javascript正規列出連結中的字串

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

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

世界只因有你世界只因有你2645 天前899

全部回覆(3)我來回復

  • PHP中文网

    PHP中文网2017-06-26 10:56:30

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

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

    回覆
    0
  • 淡淡烟草味

    淡淡烟草味2017-06-26 10:56:30

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

    回覆
    0
  • 给我你的怀抱

    给我你的怀抱2017-06-26 10:56:30

    樓上的思路不錯,把前面的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"]

    回覆
    0
  • 取消回覆