Home > Web Front-end > JS Tutorial > js regular expression matches numbers, letters, underscores, etc._javascript skills

js regular expression matches numbers, letters, underscores, etc._javascript skills

WBOY
Release: 2016-05-16 16:04:09
Original
2544 people have browsed it
1、一个正则表达式,只含有汉字、数字、字母、下划线不能以下划线开头和结尾:
^(?!_)(?!.*?_$)[a-zA-Z0-9_\u4e00-\u9fa5]+$ 其中:
^ 与字符串开始的地方匹配
(?!_)  不能以_开头
(?!.*?_$)  不能以_结尾
[a-zA-Z0-9_\u4e00-\u9fa5]+  至少一个汉字、数字、字母、下划线
$  与字符串结束的地方匹配
 
放在程序里前面加@,否则需要\进行转义 @"^(?!_)(?!.*?_$)[a-zA-Z0-9_\u4e00-\u9fa5]+$"
 (或者:@"^(&#63;!_)\w*(&#63;<!_)$"  或者 @" ^[\u4E00-\u9FA50-9a-zA-Z_]+$ " )
 
2、只含有汉字、数字、字母、下划线,下划线位置不限:
 ^[a-zA-Z0-9_\u4e00-\u9fa5]+$
 
3、由数字、26个英文字母或者下划线组成的字符串
^\w+$
 
4、2~4个汉字
 @"^[\u4E00-\u9FA5]{2,4}$"; 
 
5、
^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$
 
用:(Abc)+  来分析: 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))  
 {  
 \\符合规则  
 }  
 else 
 {  
 \\存在非法字符  
 }
Copy after login

以上所述就是本文的全部内容了,希望对大家学习javascript正则表达式能够有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template