首页  >  问答  >  正文

正则表达式 - javascript正则列出链接中的字符串

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

将链接中每个 / 后面的字符查找出来,放到一个数组里,如: ['one','two','three','four']
链接长度不限制。
正则该怎么写?

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

全部回复(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
  • 取消回复