What's the regex for matching relative path without '../'?

Viewed 36

I cannot figure out the regex pattern for my particular case. On an input form, I'm trying to validate a URI and do not want the client to be able to input either any absolute path or a relative path if it contains '../'.

I have no idea why this one is stumping... so far I've got ^([^.{2}/])(?!\.\.\/), but this one doesn't detect if the URIs have an arbitrary number of '../' strings in the middle of it. I tried ^([^.{2}/])(?!(\.\.\/)+) as well, but for some reason the URIs with '../' in the middle still fail.

My tests are:

(BAD)
/my/path
../my/path
/my/../path
my/../path
my/../../path
my/../path/../is/bad

(GOOD)
my/path
./my/path
my/./path

I appreciate the help!

1 Answers

Use

^(?!\/)(?!.*(?:^|\/)\.\.\/).+

See proof

EXPLANATION

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
    \/                       '/'
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
    (?:                      group, but do not capture:
--------------------------------------------------------------------------------
      ^                        the beginning of the string
--------------------------------------------------------------------------------
     |                        OR
--------------------------------------------------------------------------------
      \/                       '/'
--------------------------------------------------------------------------------
    )                        end of grouping
--------------------------------------------------------------------------------
    \.                       '.'
--------------------------------------------------------------------------------
    \.                       '.'
--------------------------------------------------------------------------------
    \/                       '/'
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  .+                       any character except \n (1 or more times
                           (matching the most amount possible))
Related