加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

正则表达式

发布时间:2020-12-13 22:40:22 所属栏目:百科 来源:网络整理
导读:只能输入数字:"^[0-9]*$"。 只能输入n位的数字:"^d{n}$"。 只能输入至少n位的数字:"^d{n,}$"。 只能输入m~n位的数字:。"^d{m,n}$" 只能输入零和非零开头的数字:"^(0|[1-9][0-9]*)$"。 只能输入有两位小数的正实数:"^[0-9]+(.[0-9]{2})?$"。 只能输入
只能输入数字:"^[0-9]*$"。
只能输入n位的数字:"^d{n}$"。
只能输入至少n位的数字:"^d{n,}$"。
只能输入m~n位的数字:。"^d{m,n}$"
只能输入零和非零开头的数字:"^(0|[1-9][0-9]*)$"。
只能输入有两位小数的正实数:"^[0-9]+(.[0-9]{2})?$"。
只能输入有1~3位小数的正实数:"^[0-9]+(.[0-9]{1,3})?$"。
只能输入非零的正整数:"^+?[1-9][0-9]*$"。
只能输入非零的负整数:"^-[1-9][]0-9"*$。
只能输入长度为3的字符:"^.{3}$"。
只能输入由26个英文字母组成的字符串:"^[A-Za-z]+$"。
只能输入由26个大写英文字母组成的字符串:"^[A-Z]+$"。
只能输入由26个小写英文字母组成的字符串:"^[a-z]+$"。
只能输入由数字和26个英文字母组成的字符串:"^[A-Za-z0-9]+$"。
只能输入由数字、26个英文字母或者下划线组成的字符串:"^w+$"。
验证用户密码:"^[a-zA-Z]w{5,17}$"正确格式为:以字母开头,长度在6~18之间,只能包含字符、数字和下划线。
验证是否含有^%&',;=?$"等字符:"[^%&',;=?$x22]+"。
只能输入汉字:"^[u4e00-u9fa5]{0,}$"

//CN:是否为空或者全部都是空格
String.prototype.isNull = function() { return this.regex(/^s*$/); }
//EN:
//CN:是否为邮箱
String.prototype.isEmail = function() { return this.regex(/^w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$/); }
//EN:
//CN:是否只由英文字母和数字和下划线组成
String.prototype.isNumberOr_Letter = function () { return this.regex(/^[0-9a-zA-Z_]+$/); }
//EN:
//CN:是否有包含特殊字符
String.prototype.isScript = function () { return this.regex(/(<[/]?script.*>)/i); }

//是否为空或者全部都是空格
String.prototype.isNull = function() { return this.regex(/^s*$/); }
//EN:
//CN:只能输入数字字母下划线,输入的长度自定义
//String.prototype.isscript = function(minlen,maxlen) { return this.regex(/^[a-zA-Z0-9_]{minlen,maxlen}$/); }
String.prototype.isscript = function() { return true; } //this.regex(/^[a-zA-Z0-9_]+$/);

//长度是否在指定的范围
String.prototype.isLen = function(minlen,maxlen) { return this.length >= minlen && this.length <= maxlen ? true : false; }
//只能输入由数字、26个英文字母或者下划线组成的字符串
String.prototype.isscript1 = function() { return this.regex(/^w+$/); }
//以字母开头,长度在6~18之间,只能包含字符、数字和下划线。(通常用在密码:)
String.prototype.ispwd = function() { return this.regex(/^[a-zA-Z]w{5,17}$/); }
//只能输入数字
String.prototype.isnum = function() { return this.regex(/^[0-9]*$/); }
//只能输入8位的数字
String.prototype.isonlynum = function() { return this.regex(/^d{8}$/); }
//是否为日期格式(yyyy/mm/dd)
String.prototype.isDate1 = function() { return this.regex(/^d{1,4}(-|/|.|年)(0?[1-9]|1[0-2])(-|/|.|月)(0?[1-9]|[12]d|3[01])(日)?$/); }
//是否为日期格式(mm/dd/yyyy)
String.prototype.isDate = function() { return this.regex(/^(0?[1-9]|1[0-2])(-|/|.|月)(0?[1-9]|[12]d|3[01])(-|/|.|日)(d{4})?$/); }
//CN:是否有包含特殊字符
String.prototype.isScript = function () { return this.regex(/(<[/]?script.*>)/i); }


/// <summary> ///Valid 的摘要说明 /// </summary> public static class Valid { #region 空的各种验证 //为空返回turn(空格键也行,null也行) public static bool isEmpty(this string str) { return str == null ? true : Regex.IsMatch(str,@"^s*$"); } public static bool isNotEmpty(this string str) { return !isEmpty(str); } //为空返回turn(空格键) public static bool isNull(this string str) { return Regex.IsMatch(str,"^ s*$"); } #endregion #region 长度的各种验证 //输入6~16位的数字返回turn(6:可以改动) public static bool isLeast(this string str) { return Regex.IsMatch(str,@"^d{6,16}$"); } #endregion #region 汉字、字母、数字、下划的各种验证 //为数字格式返回turn public static bool isNumber(this string str) { return Regex.IsMatch(str,@"^d+$"); } //为数字和26个英文字母组成的字符串 返回turn public static bool isNumberOrLetter(this string str) { return Regex.IsMatch(str,"^[0-9a-zA-Z]+$"); } //为数字、26个英文字母或者下划线组成的字符串 返回turn public static bool isNumberOrLetterOrUnderscore(this string str) { return Regex.IsMatch(str,@"^w+$"); } //以字母开头,长度在6~18之间,只能包含字符、数字和下划线。返回turn public static bool isNumberOrLetterOrUnderscore1(this string str) { return Regex.IsMatch(str,@"^[a-zA-Z]w{3,16}$"); } //长度在6~18之间,只能包含字符、数字和下划线。返回turn public static bool isNumberOrLetterOrUnderscore2(this string str) { return Regex.IsMatch(str,@"^w{3,16}$"); } //为汉字、字母、数字 返回turn public static bool isChinaOrLett(this string str) { return Regex.IsMatch(str,@"^[wu4e00-u9fa5]+$"); } //为汉字、字母 返回turn public static bool isChinaOrNumbOrLett(this string str) { return Regex.IsMatch(str,@"^[a-zA-Zu4e00-u9fa5]+$"); } //以汉字、字母、数字、下划线组成(不能以数字开头) 返回turn public static bool isNoNumfirst(this string str) { return Regex.IsMatch(str,@"^[a-zA-Z_u4e00-u9fa5][0-9a-zA-Z_u4e00-u9fa5]+$"); } //以!、@、#、$、%、&、*、汉字、字母、数字、下划线组成 返回turn public static bool isScpritOrOther(this string str) { return Regex.IsMatch(str,@"^[%,#,*,$,@,!,&-9a-zA-Z_u4e00-u9fa5]{1,16}$"); } #endregion #region 特殊字符的各种验证 //为‘< >’返回turn public static bool isSpChar(this string str) { return Regex.IsMatch(str,"(<|>)"); } // 验证是否含有特殊等字符 返回turn //你自己需要什么特殊符号加个‘,#’就可以了 //@"^[,`,~,',"",=,<,>,#]+$" public static bool isScript(this string str) { return Regex.IsMatch(str,@"^[,&w]+$"); } #endregion //是否为身份证号 public static bool isPID(this string str) { return Regex.IsMatch(str,@"(^d{15}$)|(^d{17}([0-9]|X|x)$)"); } //是否为邮箱格式 public static bool isEmail(this string str) { return Regex.IsMatch(str,@"^w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$"); } //是否为电话格式 //正确格式为:"XXX-XXXXXXX"、"XXX-XXXXXXXX"、"XXXX- XXXXXXX"、"XXXX- XXXXXXXX" public static bool isPhone(this string str) { return Regex.IsMatch(str,@"^(0d{2,3}-)?d{7,8}(-d{1,4})?$"); } //是否为手机格式 public static bool isMobile(this string str) { return Regex.IsMatch(str,@"^1(3|5|8)d{9}$"); } //是否为传真格式 public static bool isFax(this string str) { return Regex.IsMatch(str,@"^86-(d{2,3}-)?([2-9]d{6,7})+(-d{1,4})?$"); } //是否为图片格式‘.jpg’ public static bool isImage(this string str) { return Regex.IsMatch(str.ToLower(),".(jpg|gif|png|jpeg)$"); } //是否为钱的格式 public static bool isMoney(this string str) { return Regex.IsMatch(str,@"^d{1,8}(,d{3})*(.d{1,2})?$"); } //是否为QQ号码格式 public static bool isQQ(this string str) { return Regex.IsMatch(str,@"^[1-9]d{4,11}$"); } //是否为日期格式 ****-**-** public static bool isDate(this string str) { return Regex.IsMatch(str,4}(-|/|.|年)(0?[1-9]|1[0-2])(-|/|.|月)(0?[1-9]|[12]d|3[01])(日)?$"); } //是否为域名格式 3d_.dfss.cn.com public static bool isDomain(this string str) { return Regex.IsMatch(str,@"^([w-]+.)+[w-]+$"); } //是否为网址格式(带 http:// | https:// | ftp:// 这个开头) public static bool isWebUrl(this string str) { return Regex.IsMatch(str,@"^(http|https|ftp)://([w-]+.)+[w-]+(/[w- ./?%&=]*)?$"); } //是否为网站地址格式(不带 http:// | https:// | ftp:// 这个开头) public static bool isUrl(this string str) { return Regex.IsMatch(str,@"^([w-]+.)+[w-]+(/[w- ./?%&=]*)?$"); } //将用户输入到控件中的非法字符转译成合法字符 public static string parseString(this string str) { if (str.IndexOf("'")!=-1) { str = str.Replace("'","'"); } if (str.IndexOf(""")!=-1) { str = str.Replace(""","“"); } if (str.IndexOf("<")!=-1) { str =str .Replace("<","<"); } if (str.IndexOf(">") != -1) { str = str.Replace(">",">"); } if (str.IndexOf("&") != -1) { str = str.Replace("&","&"); } return str; } }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读