简体   繁体   中英

Regular expression to validate the user input

I want a regular expression, which should start with alphabet or numbers and followed by alphanumeric character and in between, it may or may not contain forward or backward slash ( \\,/ ).

Highly appreciate your help.

Thanks :)

/^[a-z0-9][\w\\\/]+$/i
  1. Start of line ( ^ ).
  2. [a-z0-9] - a letter or number - 1 occurrence
  3. [\\w\\\\\\/]+ - multiple occurrences of alphanumeric characters (including _ ) or \\ or / .
  4. Line end ( $ ).

i gnore-case flag will accept both uppercase and lowercase.

[xyz] specifies a character class meaning that either x or y or z can be matched.

DEMO

If you don't consider 123_asd to be alpha-numeric, use:

/^[a-z0-9][a-z0-9\\\/]+$/i

Hope it helps!

^[a-zA-Z0-9]+[a-zA-Z0-9\/\\]*

This will accept / or \\ only in between.

Edit: Oops:p Missed the numbers, updated. Thanks for pointing out.

Try this regex:

(?i)^[a-z\d][a-z\d\\/]*$

?i treats upper-case letters as lower-case letters.

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