search

Home  >  Q&A  >  body text

javascript - js string interception

'/xxx/xxx/xxx/xxx/xxx/...', /xxx is greater than or equal to 2, how to get the first two '/xxx/xxx' in the simplest way (without defining extra variables) ?

代言代言2724 days ago791

reply all(4)I'll reply

  • PHP中文网

    PHP中文网2017-06-12 09:29:43

    a = a.split("/")
    a[1]
    a[2]

    reply
    0
  • 欧阳克

    欧阳克2017-06-12 09:29:43

    '/' + '/product/list/xxx/xxx'.slice(1).split('/', 2).join('/')

    reply
    0
  • 怪我咯

    怪我咯2017-06-12 09:29:43

    ("/xxx/xxx/xxx/xxx/xxx/...".match(/^\/.*?\/[^\/]*/) || [])[0]

    reply
    0
  • 我想大声告诉你

    我想大声告诉你2017-06-12 09:29:43

    使用正则/(\/.*?\/.*?)\//
    
    var str = '/xxx/xxx/xxx/xxx/xxx/...';
    var reg = /(\/.*?\/.*?)\//;
    console.log(str.match(reg)[1])

    reply
    0
  • Cancelreply