Jessica M.
Jessica M.

Reputation: 13

Validating a url path with regex and excluding special characters

I'm trying to write up an expression that starts with a '#" and takes in the following types of paths and only takes in a character A-z/a-z and doesn't accept digits or special characters: Example valid paths:

#/
#/word/word
#/word
#/word/word/word

This is what I have currently:

#\/\D+|\/\D+

I have also tried:

#\/\D+|\s\/^[A-Za-z\s]*$

It filters 85% of the paths correctly but it still accepts paths with special characters as valid such as "#/word/word?test=word" "#/word/word=%"

I'm not quite sure what I am missing.

Upvotes: 1

Views: 1003

Answers (2)

The fourth bird
The fourth bird

Reputation: 163467

You can start the match with #/ and then optionally match the trailing part:

^#\/(?:[A-Za-z]+(?:\/[A-Za-z]+)*)?$

Explanation

  • ^ Start of string
  • #\/ Match #/
  • (?: Non capture group
    • [A-Za-z]+ Match 1+ chars A-Za-z
    • (?:\/[A-Za-z]+)* Optionally repeat / and 1+ chars A-Za-z
  • )? Close the non capture group and make it optional
  • $ End of string

See a regex demo.

Upvotes: 0

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522151

I would phrase the regex as:

^(?:#/|#/[A-Za-z]+(?:/[A-Za-z]+)*)$

This regex says to match:

  • ^ from the start of the string
  • (?:
    • #/ match #/ by itself
    • | OR
    • #/ match #/
    • [A-Za-z]+ followed by a path name
    • (?:/[A-Za-z]+)* followed by zero or more other paths
  • )
  • $ end of the string

Demo

Upvotes: 0

Related Questions