Home > OS >  Regex to allow only a single slash but not multiple slashes
Regex to allow only a single slash but not multiple slashes

Time:02-02

enter image description hereI am writing a regex to satisfy the following conditions no capital letters, no spaces, no underscores, umlauts or special characters and no multiple slashes.

I have implemented this /[^a-z\/{1}\s]/g but still it allows more than one slash. Any idea what I am missing .

if(value.match(/[^a-z\/{1}\s]/g) ){
    console.log(invalid)
 }
  1. these values should fail to pass a test

  SHOULDNOTMATCH
  some_
  über
  ässs
  schleißheim
  some?sd
  some/7asa 
  some one
  shouldnotmatche//
  dont'allow
  /home//some/where/
  1. These values should pass a test

   home
   page
   thisshouldpass
   someothertext

CodePudding user response:

Converting my comment to answer so that solution is easy to find for future visitors.

You may consider this regex for your cases:

/^[a-z] (?:\/[a-z]*)?$/igm

Which matches 1 letters at the start i.e. ^[a-z] followed by an optional group: (?:\/[a-z]*)? which means match a / followed by 0 or more letters before end position.

RegEx Demo

CodePudding user response:

See Demo for some explanations. As stated the explanation is behind the link.

  •  Tags:  
  • Related