简体   繁体   English

正则表达式以验证用户输入

[英]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 [a-z0-9] -一个字母或数字-1次出现
  3. [\\w\\\\\\/]+ - multiple occurrences of alphanumeric characters (including _ ) or \\ or / . [\\w\\\\\\/]+ -多次出现的字母数字字符(包括_ )或\\/
  4. Line end ( $ ). 行尾( $ )。

i gnore-case flag will accept both uppercase and lowercase. i忽略大小写标志将接受大写和小写。

[xyz] specifies a character class meaning that either x or y or z can be matched. [xyz]指定一个字符类,表示可以匹配xyz

DEMO DEMO

If you don't consider 123_asd to be alpha-numeric, use: 如果您不认为123_asd是字母数字,请使用:

/^[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. 编辑:糟糕:p缺少数字,已更新。 Thanks for pointing out. 感谢您指出。

Try this regex: 试试这个正则表达式:

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

?i treats upper-case letters as lower-case letters. ?i将大写字母视为小写字母。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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