Regex to not match more than one trailing slash in string

Looking for a regex to not match more than 1 occurrence of a trailing slash


Expected match


What I tried:


>Solution :

In the pattern that you tried, the second part of the pattern can not match, it asserts the start of the string ^ and then matches a single character in the character class (^[\/{2,}\s])$ directly followed by asserting the end of the string.



But you have already asserted the start of the string here ^\/

You can repeat a pattern starting with / followed by repeating 1+ times the character class that you already have:



  • ^ Start of string
  • (?:\/[^\/?&#\s]+)+ Repeat 1+ times / and 1+ char other than the listed ones
  • \/? Optional /
  • $ End of string

See a regex demo

Leave a Reply