JS 和 C#使用正则表达式

本文提供了一系列常用的正则表达式,包括验证中文字符、电子邮件、URL等,并给出了JS及C#下的实现示例。

JS的正则表达式
function isDigit(s)
{
var patrn=/要验证的正则表达式$/;//这个格式很重要;
if (!patrn.exec(s)) return false
return true
}

C#的正则表达式

这里是一个很常见的例子 是验证是不是有中文字符的对象!

/// <summary> /// 这是判断是不是含有中文的方法 /// </summary> /// <param name="Source">传来的字符串</param> /// <returns>是否有中文字符 true --有中文 false--没有中文</returns> public static bool IsContentChinese(string Source) { Regex rx = new Regex(@"[/u4e00-/u9fa5]"); return rx.IsMatch(Source); }


收录的有用的的正则表达式

private static string ValidateChinaese = @"[/u4e00-/u9fa5]";//中文验证 private static string ValidateDoubleChar = @"[^/x00-/xff]";//匹配双字节 private static string ValidateBlankChar = @"/n/s*/r";//空白字符 private static string ValidateHtml = @"<(/S*?)[^>]*>.*?<//1>|<.*? />";//匹配HTML格式 private static string ValidateStartEndBlank = @"^/s*|/s*$";//匹配首尾是空白字符 private static string ValidateEmail = @"/w+([-+.]/w+)*@/w+([-.]/w+)*/./w+([-.]/w+)*";//匹配电子邮件 private static string ValidateUrl = @"[a-zA-z]+://[^/s]*";//匹配url地址 private static string ValidateUserID = @"^[a-zA-Z][a-zA-Z0-9_]{4,15}$";//匹配用户名 长度是4-15的长度 可控性差 但是 可以当成是一个事例来写正则表达式 private static string ValidateTel = @"(86-)?/d{3}-/d{8}|/d{4}-/d{8}";//国内的电话号码 这也可以用于国际中 +86 可以不输 private static string Validate10086 = @"^1(3[4-9]|5[012789]|8[78])/d{8}$";//移动手机 private static string Valiate10010 = @"^1(3[0-2]|5[56]|8[56])/d{8}$";//中国联通 private static string Valiate10000 = @"^1(3[0-2]|5[56]|8[56])/d{8}$";//中国电信 private static string Validate100002=@"^18[09]/d{8}$";//电信号还有180和189的号 private static string ValiateCDMA = @"^1[35]3/d{8}$";//CDMA 用户 private static string ValidateQQ = @"[1-9][0-9]{4,}";//腾讯QQ号码 private static string ValidatePost = @"[1-9]/d{5}(?!/d)";//邮政编码 private static string ValidateIDCard = @"/d{15}|/d{18}";//身份证验证 private static string ValidateIpAddress = @"/d+/./d+/./d+/./d+";//IP地址验证对象 private static string ValidatePlus = @"^[1-9]/d*$";    //匹配正整数 private static string ValidateNegative = @"^-[1-9]/d*$";   //匹配负整数 private static string ValidateInteger = @"^-?[1-9]/d*$";   //匹配整数 private static string ValidateNotInteger = @"^[1-9]/d*|0$";  //匹配非负整数(正整数 + 0) private static string ValidateNotPlus = @"^-[1-9]/d*|0$";   //匹配非正整数(负整数 + 0) private static string ValidateFloat = @"!^[1-9]/d*/./d*|0/./d*[1-9]/d*$";  //匹配正浮点数 private static string ValidateNotFloat = @"^-([1-9]/d*/./d*|0/./d*[1-9]/d*)$";  //匹配负浮点数 注: 下面的是用在js中的 这里只是将这个正则表达式列出来 private static string ValidateDate=var s = /((^((1[8-9]/d{2})|([2-9]/d{3}))([-///._])(10|12|0?[13578])([-///._])(3[01]|[12][0-9]|0?[1-9]))|(^((1[8-9]/d{2})|([2-9]/d{3}))([-///._])(11|0?[469])([-///._])(30|[12][0-9]|0?[1-9]))|(^((1[8-9]/d{2})|([2-9]/d{3}))([-///._])(0?2)([-///._])(2[0-8]|1[0-9]|0?[1-9]))|(^([2468][048]00)([-///._])(0?2)([-///._])(29))|(^([3579][26]00)([-///._])(0?2)([-///._])(29))|(^([1][89][0][48])([-///._])(0?2)([-///._])(29))|(^([2-9][0-9][0][48])([-///._])(0?2)([-///._])(29))|(^([1][89][2468][048])([-///._])(0?2)([-///._])(29))|(^([2-9][0-9][2468][048])([-///._])(0?2)([-///._])(29))|(^([1][89][13579][26])([-///._])(0?2)([-///._])(29))|(^([2-9][0-9][13579][26])([-///._])(0?2)([-///._])(29) ))/s((20|21|22|23|[0-1]?/d):[0-5]?/d:[0-5]?/d)$/;

备用 挺好!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值