search

Home  >  Q&A  >  body text

Regular expression - javascript regular expression to list strings in links

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

Find the characters after each / in the link and put them into an array, such as: ['one','two','three','four']
There is no limit on link length.
How to write regular expression?

世界只因有你世界只因有你2697 days ago961

reply all(3)I'll reply

  • PHP中文网

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

    is the current page url: window.location.pathname.substr(1).split('/')

    Not the current page url: url.replace(/http(s){0,1}://[^/]+//, '').split('/')

    reply
    0
  • 淡淡烟草味

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

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

    reply
    0
  • 给我你的怀抱

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

    The idea upstairs is good, remove the host in front, and use / to divide the rest, which is simpler

    ------Here are the answers

    This requires the use of assertions

    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"]

    reply
    0
  • Cancelreply