Jo Po
Jo Po

Reputation: 3

Regex for validate repeating slash

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

Answers (1)

A.Hristov
A.Hristov

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

Related Questions