常用正则表达式总结(js与C#对照)
生活随笔
收集整理的這篇文章主要介紹了
常用正则表达式总结(js与C#对照)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
js用r.test()而C#用regex.IsMatch()來驗證正則。
大氣象 <%@?Page?Language="C#"?AutoEventWireup="true"?CodeFile="RegexTest.aspx.cs"?Inherits="RegexTest"?%><!DOCTYPE?html?PUBLIC?"-//W3C//DTD?XHTML?1.0?Transitional//EN"?"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html?xmlns="http://www.w3.org/1999/xhtml"?>
<head?runat="server">
????<title>常用正則表達式總結</title>
????<script?type="text/javascript"?src="js/jquery-1.3.2.min.js"></script>
????<script?type="text/javascript">
????????function?fCheck(){
????????????//檢查用戶名
????????????var?uname?=?jQuery("#txtUserName").val();
????????????if(uname?==?""){alert('用戶名不可為空!');return?false;}
????????????r=/^[\w\d]{4,16}$/;
????????????if(!r.test(uname)){alert('用戶名為4-16個字符!');return?false;}
????????????//檢查密碼
????????????var?upwd?=?jQuery("#txtPwd").val();
????????????if(upwd?==?""){alert('密碼不可為空!');return?false;}
????????????r=/^.{6,14}$/;
????????????if(!r.test(upwd)){alert('密碼為6-14個字符!');return?false;}
????????????if(jQuery("#txtPwdConfirm").val()!=upwd){alert('兩次輸入的密碼不一致!');return?false;}
????????????//檢查郵箱
????????????var?uemail?=?jQuery("#txtEmail").val();
????????????if(uemail==""){alert('電子郵件地址不能為空!');return?false;}
????????????r=/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
????????????if(!r.test(uemail)){alert('電子郵件地址格式不正確!');return?false;}
????????????
????????????return?true;
????????}
????</script>
</head>
<body>
????<form?id="form1"?runat="server">
????<div>
????用戶名:
????<asp:TextBox?ID="txtUserName"?runat="server"></asp:TextBox>
????4-16個字符(數字,字母和下劃線)<br?/>
????
????密碼及確認密碼:<asp:TextBox?ID="txtPwd"?runat="server"?TextMode="password"?Width="80px"></asp:TextBox>?
????<asp:TextBox?ID="txtPwdConfirm"?runat="server"?TextMode="password"?Width="80px"></asp:TextBox>
????<br?/>
????
????電子郵件地址:
????<asp:TextBox?ID="txtEmail"?runat="server"></asp:TextBox>(請輸入有效的郵件地址,當密碼遺失時憑此領取)
????
????<br?/>
????<asp:Button?OnClientClick=""?ID="btnRegister"?runat="server"?Text="同意以下協議并注冊"?OnClick="btnRegister_Click"?/>
????</div>
????</form>
</body>
</html>
?
?
大氣象 using?System;using?System.Data;
using?System.Configuration;
using?System.Collections;
using?System.Web;
using?System.Web.Security;
using?System.Web.UI;
using?System.Web.UI.WebControls;
using?System.Web.UI.WebControls.WebParts;
using?System.Web.UI.HtmlControls;
using?System.Text.RegularExpressions;
public?partial?class?RegexTest?:?System.Web.UI.Page
{
????protected?void?Page_Load(object?sender,?EventArgs?e)
????{
????}
????/****************************************************
?????*?^匹配開始?$匹配結束
?????*?{m,n}匹配從m到n個前面的表達式,比如[\w]{4,16}匹配4到16個字母
?????*?.匹配除?"\n"?之外的任何單個字符。
?????*?要匹配包括?'\n'?在內的任何字符,請使用象?'[.\n]'?的模式。
?????*?[-+.]三個符號,只能有一個。
?????***************************************************/
????protected?void?btnRegister_Click(object?sender,?EventArgs?e)
????{
????????//用戶名為4-16個字符(數字,字母和下劃線)
????????Regex?regex?=?new?Regex(@"^[\w\d]{4,16}$");
????????if(!regex.IsMatch(txtUserName.Text.Trim()))
????????{
????????????Page.ClientScript.RegisterClientScriptBlock(this.GetType(),?"alert",?"<script>alert('用戶名為4-16個字符(數字,字母和下劃線)。');</script>",?false);
????????????return;
????????}
????????//密碼為6-14個字符!
????????regex?=?new?Regex(@"^.{6,14}$");
????????if?(!regex.IsMatch(txtPwd.Text.Trim()))
????????{
????????????Page.ClientScript.RegisterClientScriptBlock(this.GetType(),?"alert",?"<script>alert('密碼為6-14個字符!');</script>",?false);
????????????return;
????????}
????????//密碼為6-14個字符!
????????regex?=?new?Regex(@"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$");
????????if?(!regex.IsMatch(txtEmail.Text.Trim()))
????????{
????????????Page.ClientScript.RegisterClientScriptBlock(this.GetType(),?"alert",?"<script>alert('電子郵件地址格式不正確!');</script>",?false);
????????????return;
????????}
????????Page.ClientScript.RegisterClientScriptBlock(this.GetType(),?"alert",?"<script>alert('注冊成功!請登錄。');window.location.href='Login.aspx';</script>",?false);
????}
}
?
?
總結
以上是生活随笔為你收集整理的常用正则表达式总结(js与C#对照)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 养殖业什么最稳成本低 创业者进来了解学习
- 下一篇: c# char unsigned_dll