3

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 :(

2 Answers2

1

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 /
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
0

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.

Fatih Aktaş
  • 1,446
  • 13
  • 25