0

I need help to extract values from string req.url.path which looks like this:

/a/b/c/d

Need to extract c, if there is url, like /a, it should return ''. I tried

regsub(req.url.path, "/a/b/", "\5");

Tried with replace too. but it is not effectively working for me. Please help me.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
pl2ern4
  • 340
  • 2
  • 10

1 Answers1

2

I believe the following might be along the lines of what you require:

if (req.url.path ~ "^/a/.+/c") {
  set req.url = regsub(req.url.path, "/c", "") + "?" + req.url.qs;
}

It checks if the URL path begins with /a and is followed by /c (presuming also that there are segments in-between, such as /b as per your given example /a/b/c/d).

Here is a Fastly Fiddle link for you to play around with the code:
https://fiddle.fastlydemo.net/fiddle/527480ba

So given the input URL path:

/a/b/c/d?id=testing

It would change to:

/a/b/d?id=testing

Notice that the /c has been extracted from the path.

Integralist
  • 495
  • 3
  • 7