简体   繁体   中英

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?

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM