-1

Did some searching on stack but couldn't find the answer to my question...

I'm looking for a regex string that extracts the following url's for my hotjar experiment. I'm not sure how i get it to work.

/vacatures
/vacatures/
/vacatures/bouw/
/vacatures/installatietechniek/

but NOT

/vacatures/bouw/everything-that-comes-after-the-third-slash

Can you guys help me out?

Many Thanks!

Thijs
  • 9
  • 1
  • Not sure why this question is closed with that particular reason, because the linked question does not address OP's question at all. – Terry Sep 04 '20 at 14:15
  • That looks like part of URL, do you want to extract strings starting with `/vacatures` in URL? – Ankit Sep 04 '20 at 14:29

1 Answers1

-1

You don't need regex to do that: you just need to split by / and then filter out empty elements from the array. When the array contains 3 or more items you know that it contained three slashes:

const urls = [
  '/vacatures', // accept
  '/vacatures/', // accept
  '/vacatures/bouw/', // accept
  '/vacatures/installatietechniek/', //accept
  '/vacatures/bouw/everything-that-comes-after-the-third-slash', // reject
  '/vacatures/bouw/everything-that-comes-after-the-third-slash/' // reject
];

function check(url) {
  const parts = url.split('/').filter(x => !!x);
  if (parts.length > 2) {
    console.log(url, 'REJECT');
  } else {
    console.log(url, 'ACCEPT');
  }
}

urls.forEach(check);
Terry
  • 48,492
  • 9
  • 72
  • 91
  • I think i need some more help, but first let me clarify why I need to do this: I would like to fire a certain hotjar expiriment that matches a regex equalling the pages in my first post! – Thijs Sep 04 '20 at 14:17