2

So, I've tried making a little Regex expression to fetch the requested URL, minus the starting an trailing slash.

One little catch, the trailing slash wont always be there, for instance if the user requests "/test/example/", they can also request "/test/example". So I tried to make a method to handle that:

req.url.match(/^(?:\/)(.+)(?:[\/])?$/i)[1]

Although, if I request a path like "/test/example/", it keeps the trailing slash, and returns "test/example/" in the capture group...? Basically what I wanted to avoid. (So, all it's doing is removing the starting slash)

Now, I tried removing the ? that's next to the $ symbol. But this just causes an error when requesting "/test/example" (something without the trailing slash), because [1] would be null.

I made an example on regex101, which you can view here. As you can see, the capture group includes the ending slash, even though in my expression, I thought I told it to not do that.

TL;DR: Regex is still capturing trailing slash, even though I don't want to do (and keep in mind that the trailing slash wont always be present).


To clairify, I want the regex to do this:

"/test/example/" to "test/example"

and

"/test/example" to "test/example"

(So, removing the starting and trailing slash, but the trailing slash is optional)

Community
  • 1
  • 1

3 Answers3

1

You need to make the regex less greedy. Add 2 ?s:

^(?:\/)?(.*?)(?:[\/])?$

See updated regex here.

Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
  • Great. I should have known. For some reason my mind kept thinking that if I used the lazy operator it would match `"/test/example"` to `"test"`... But then I just remembered I was using the end of string `$` operator. –  Mar 01 '15 at 22:15
  • As for the first `?` you added, that doesn't matter, because it will always be there in the NodeJS req.url. –  Mar 01 '15 at 22:17
  • Is the req.url ever fully written like `http://domain.com/test/example/` ? If so, this will not match properly. Just trying to think of any possible issues. If that is never the case, then you don't need to match anything and you can just do `req.url.replace(/(^\/|\/$)/g,'')` – FactoryAidan Mar 01 '15 at 22:22
  • No, it only gives the requested path... Like in my examples: `"/test/example"`. But no matter what path you request, there will always be a starting `"/"`, so the `?` on the first group doesn't really matter. –  Mar 01 '15 at 23:02
  • @JamenMarz: I made the first slash optional to meet the "/test/example/" to "test/example" requirement. – Wiktor Stribiżew Mar 01 '15 at 23:27
0

use this pattern and replace with nothing

^\/|\/$  

Demo

alpha bravo
  • 7,292
  • 1
  • 14
  • 22
0

Javascript's regex engine is not the strongest.

I had an issue just like this and I ended up doing the regex in two steps for better readability and predictability.

var matches = req.url.match(/^(?:\/)(.+)(?:[\/])?$/i)

if(matches.length){
    var my_url = matches[1].replace(/(^\/|\/$)/g,'')    //  Removes start and ending slashes
}else{
    var my_url = 'something_else'
}
FactoryAidan
  • 2,320
  • 1
  • 11
  • 12