Home > front end >  matching a folder path with regex with forward slash
matching a folder path with regex with forward slash

Time:01-25

I have a following regex to match a folder name. What I am trying to get is to match a string with letter, digits, dot, hyphen, underscore and forward slash such that the string doesn't need to start or end with forward stash or dot:

/^([a-zA-Z0-9_][a-zA-Z0-9.\/_\-])[.a-zA-Z0-9_-]{1,253}[^(.|/|\\)]$/

I need to match folder name foo.com/sdsds.

But the regular expression is not matching /.

CodePudding user response:

You can use

^(?![\/.])[\w.\/-]{1,253}$(?<![.\/])

See the regex demo. Details:

  • ^ - start of string
  • (?![\/.]) - no / or . allowed immediately to the right of the current location
  • [\w.\/-]{1,253} - one to 123 digits, letters, underscores, ., / or - chars
  • $(?<![.\/]) - end of string with no . or / immediately preceding it.
  •  Tags:  
  • Related