Reputation: 3
I am trying to write a regexp (javascript) to validate a string where slashes are repeated.
Must be:
/ - valid string
/abs - valid string
/abs/ - valid string
/abs/a - valid string
// - invalid string
//asd//sd/ -invalid string
/as//a - invalid string
a/a - invalid string
abs - invalid string
////////sdf///// - invalid string
I am trying this regexp:
(\/){1}[\w-]*
^(\/){1}[\w-]*
^(\/\w+)+(\.)?\w+(\?(\w+=[\w\d]+(&\w+=[\w\d]+)*)+){0,1}$
How do I write an expression to pass the validation criteria?
Upvotes: 0
Views: 767
Reputation: 483
This is a pretty simple regex and should work for any engine:
^\/[^\/]+(?:\/[^\/]+)*$
The idea is that it should always start with a forward slash and no two forward slashes should touch. You can test it here: https://regex101.com/r/1BCQs3/2/
Upvotes: 1