简体   繁体   中英

regex URL extract slash

I need to extract the middle number from this using regex:

/166012342170/934760332134/794294808150/2436280815794/

Needed output: 934760332134

I tried using this: (?:.+?/){2}

I am new to regex :(

In JS, you may simply split the string with / char:

 console.log('/166012342170/934760332134/794294808150/2436280815794/'.split('/')[2]) 

If you plan to use regex you may use

 var s = '/166012342170/934760332134/794294808150/2436280815794/'; var m = s.match(/^\\/[^\\/]+\\/([^\\/]+)/); if (m) { console.log(m[1]); } 

Details

  • ^ - start of string
  • \\/ - a / char
  • [^\\/]+ - 1+ chars other than /
  • \\/ - a / char
  • ([^\\/]+) - Capturing group 1: 1+ chars other than /

To get that second number, the regex would be

(?:\/([0-9]+)){2}

which gives 934760332134 from the line below,

/166012342170/934760332134/794294808150/2436280815794

Try it on here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM