js regular expressions match alphanumeric underscores and so on

  • 2020-05-27 04:26:04
  • OfStack


1 , 1 The regular expression contains only Chinese characters, Numbers and letters, and the underscore cannot be followed by the beginning and end of the underscore: 
^(?!_)(?!.*?_$)[a-zA-Z0-9_\u4e00-\u9fa5]+$  Among them: 
^  Matches where the string begins 
(?!_) Not to _ At the beginning 
(?!.*?_$) Not to _ At the end 
[a-zA-Z0-9_\u4e00-\u9fa5]+ At least, 1 Chinese characters, Numbers, letters, underline 
$ Matches where the string ends 
 
 Put it in front of the program @ , otherwise \\ escape  @"^(?!_)(?!.*?_$)[a-zA-Z0-9_\u4e00-\u9fa5]+$"
  (or: @"^(?!_)\w*(?<!_)$"   or  @" ^[\u4E00-\u9FA50-9a-zA-Z_]+$ " )
 
2 , only contains Chinese characters, Numbers, letters, underline, underline position is not limited: 
 ^[a-zA-Z0-9_\u4e00-\u9fa5]+$
 
3 , by Numbers, 26 A string of letters or underscores 
^\w+$
 
4 , 2~4 The Chinese characters 
 @"^[\u4E00-\u9FA5]{2,4}$"; 
 
5 , 
^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$
 
 To use: (Abc)+   To analyze:  XYZAbcAbcAbcXYZAbcAb
 
XYZAbcAbcAbcXYZAbcAb
6 , 
[^\u4E00-\u9FA50-9a-zA-Z_]
34555#5' -->34555#5'
 
[\u4E00-\u9FA50-9a-zA-Z_]  eiieng_89_  --->  eiieng_89_
_';'eiieng_88&*9_  --> _';'eiieng_88&*9_
_';'eiieng_88_&*9_ --> _';'eiieng_88_&*9_
 
public bool RegexName(string str)
 {
  bool flag=Regex.IsMatch(str,@"^[a-zA-Z0-9_\u4e00-\u9fa5]+$");
  return flag;
 }
 
 Regex  reg=new  Regex("^[a-zA-Z_0-9]+$");  
 if(reg.IsMatch(s))  
 {  
 \\ Comply with the rules   
 }  
 else 
 {  
 \\ Invalid character   
 }

That's all for this article, and I hope it will be helpful for you to learn javascript regular expressions.


Related articles: