URL matching regex

I need my regex to match both links below but unfortunately, it only matches the second one. I think the ? should mean 0 or more chances of occuring but it doesn’t apply that way in my regex. The regex is as follows:


And the links are below

  1. /login/file.css
  2. /login

it works for me for both paths.

Well, see for yourself in your browser dev console. It always outputs null (meaning no match found) for the first string.

/\/([\w-]+)(\/[a-z]+\.([a-z]+))?$/.test('/login/files.css') // true
/\/([\w-]+)(\/[a-z]+\.([a-z]+))?$/.test('/login') // true

'/login/files.css'.match(/\/([\w-]+)(\/[a-z]+\.([a-z]+))?$/gi) // ["/login/files.css"] (1) = $2

Kindly explain how I can get it to work here https://jsbin.com/juxayoc/edit?js,console,output as desired i.e output the array of matches for both strings.


This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.