荣耀彩票代理

  • 热门专题

正则表达式详解

作者:hackbase  发布日期:2015-08-26 22:42:06
  • 荣耀彩票代理ZHENGZEBIAODASHI,YOUCHENGZHENGGUIBIAOSHIFA、CHANGGUIBIAOSHIFA(YINGYU:Regular Expression,ZAIDAIMAZHONGCHANGJIANXIEWEIregex、regexpHUORE),JISUANJIKEXUEDEYIGEGAINIAN。ZHENGZEBIAODASHISHIYONGDANGEZIFUCHUANLAIMIAOSHU、PIPEIYIXILIEFUHEMOUGE JUFAGUIZEDEZIFUCHUAN。ZAIHENDUOWENBENBIANJIQILI,ZHENGZEBIAODASHITONGCHANGBEIYONGLAIJIANSUO、TIHUANNEIXIEFUHEMOUGEMOSHIDEWENBEN。

    正则表达式可以干嘛?

    1、SHUJUYOUXIAOXINGYANZHENG,KEYIDUIYIGEZIFUCHUANJINXINGCESHI,KANGAIZIFUCHUANSHIFOUFUHEMOUZHONGGUIDING。

    2、TIHUANWENBEN,KEYIZAIWENDANGZHONGJIANGZHIDINGDEWENZISHANCHU,HUOTIHUANWEIQITAWENZI。

    荣耀彩票代理3、GENJUZIFUCHUANTIQUYIGEZIZIFUCHUAN,YONGLAIZAIWENBENHUOSHURUZIDUANZHONGCHAZHAOTEDINGWENZI。

    正则表达式语法

    YIGEZHENGZEBIAODASHISHIYOUPUTONGZIFU(a-z)HETESHUZIFU(YUANZIFU)ZUCHENGDEWENZIMOSHI,GAIMOSHIZUOWEIYIGEMOBAN,JIANGMOUGEZIFUMOSHIYUSUOSOUSUODEZIFUCHUANJINXINGPIPEI。

    元字符

    JUYOUTESHUYIYIDEZHUANYONGZIFU。

    基本元字符

       .  PIPEICHUHUANXINGYIWAIDERENYIZIFU,LIRUZHENGZEBIAODASHI r.t NENGGOUPIPEI “rat、rut、r t”,DANSHIBUNENGPIPEIroot 

    荣耀彩票代理    [] PIPEIPIPEIYIGECHUXIANZAI[]ZHONGDEZIFU 

        |  HUO MINGANCI ab|cd|ed|df

    荣耀彩票代理    () TIGAOYOUXIANJI a(bc) SHIXIANFENZU

    限定元字符

        *     ZHONGFULINGCIHUODUOCI

    荣耀彩票代理    +     ZHONGFUYICIHUODUOCI

        ?     ZHONGFULINGCIHUOYICI

        {n}   ZHONGFUnCI

        {n,}  ZHONGFUnCIHUODUOCI

        {n,m} ZHONGFUnDAOmCI

    首尾元字符

        ^ PIPEIZIFUCHUANKAISHI LIRUZHENGZEBIAODASHI ^when NENGGOUPIPEIDAO ”when in the“ DEKAISHI,DANBUNENGPIPEIDAO ”what and when in the“ 

        $ PIPEIZIFUCHUANJIESHU LIRUZHENGZEBIAODASHI food$ NENGGOUPIPEIDAO “he's  food” DEMOWEI

    简写形式

         DANCIDEKAISHIHUOJIESHU

        d PIPEI0-9DESHUZI  DFEISHUZI

    荣耀彩票代理    s RENYIDEKONGBAIFU BAOKUOtabHEHUANXINGFU SDAXIE FEI KONGBAIZHIWAIDESUOYOUZIFU

    荣耀彩票代理    w PIPEIZIMU、SHUZIHUOXIAHUAXIAN W FEIZIMU、SHUZIXIAHUAXIAN

    转义字符

        RUGUOXIANGCHAZHAOYUANZIFUBENSHENDEHUA,BIRUXIANGCHAZHAO.HUOZHE*JIUHUICHUXIANWENTI,YINWEIHUIJIESHICHENGQITAYISI。ZHEISHIJIUXUYAOSHIYONGLAIQUXIAOZHEIXIEZIFUDETESHUYIYI。

        YINCIXUYAOSHIYONG. * CHAZHAOBENSHENYINGGAIXIE\

    在javascript中使用正则表达式

    CHUANGJIANZHENGZEBIAODASHI

    ?1234//字面值var reg = /d+/;//构造函数var regx = new RegExp(<a href='file://\d+'>\d+a>);

    匹配

    ?123456789if(reg.test('45646515')){  alert('OK');} if(regx.test('sd')){  alert('ok')}

    提取

    ?12345678var str='853020304@qq.com';var reg = /([a-zA-Z0-9]+)@([a-zA-Z0-9]+(.[a-zA-Z0-9]+)+)/; var match = reg.exec(str);alert(match[0]); //853020304@qq.com 匹配到的对象alert(match[1]); //853020304 [1][2]分别是各个组alert(match[2]); //qq.comalert(match[3]); //.com

    循环提取

    ?12345678910111213141516var str = '12345';var r = /dd/g; //g global 全局匹配var m = r.exec(str); //12alert(m);m = r.exec(str); //34alert(m);m = r.exec(str); //null 没有了返回nullalert(m); var arr = [];var m = null;while((m=r.exec(str)) != null) //如果不等于null 继续循环{  arr.push(m); //添加到数组}alert(arr.length); //[0]=12 [1]=34

    替换

    ?123456789string str = <word">string>.replace(正则表达式或字符串,替换为某字符串); var str = 'ab--c--d--e--f-g';var result = str.replace(/-+/g,','); //如果不写g 则只替换第一个alert(result); //ab,c,d,e,f,g var date = '2015年8月24日';var result = date.replace(/(d+)年(d+)月(d+)日/,'$1-$2-$3');alert(result);//2015-8-24

    在c#中使用正则表达式

    ?12345var regx = '^[a-zA-Z0-9]{6,20}$';word">if ( ! Regex.IsMatch('abcdef;sd123',regex){  //长度必须6-20,字母和数字}

    常用表达式

    荣耀彩票代理PIPEISHENFENZHENG:d{15}|d{18}

    荣耀彩票代理PIPEIZHONGGUOYOUZHENGBIANMA:[1-9]d{5}(?!d)

    PIPEITENGXUNQQHAO:[1-9][0-9]{4,}

    PIPEIGUONEIDIANHUAHAOMA:d{3}-d{8}|d{4}-d{7}

    荣耀彩票代理PIPEIZHANGHAOSHIFOUHEFA(ZIMUKAITOU,YUNXU5-16ZIJIE,YUNXUZIMUSHUZIXIAHUAXIAN):^[a-zA-Z][a-zA-Z0-9_]{4,15}$

    PIPEIWANGZHIURLDEZHENGZEBIAODASHI:[a-zA-z]+://[^s]*

    PIPEIEmailDIZHIDEZHENGZEBIAODASHI:w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*

    PIPEISHOUWEIKONGBAIZIFUDEZHENGZEBIAODASHI:^s*|s*$

    匹配HTML标记的正则表达式:<(S*?)[^>]*>.*?|<.*? />

    荣耀彩票代理PIPEIZHONGWENZIFUDEZHENGZEBIAODASHI: [YI-YUE]

    限制网页表单里的文本框输入内容

    ZHINENGSHURUZHONGWEN:

    ?1'text' onkeyup='value=value.replace(/[^一-龥]/g,'')' onbeforepaste='clipboardData.setData('text',clipboardData.getData('text').replace(/[^一-龥]/g,''))' />

    ZHINENGSHURUSHUZI:

    ?1'text' onkeyup='value=value.replace(/[^d]/g,'') ' onbeforepaste='clipboardData.setData('text',clipboardData.getData('text').replace(/[^d]/g,''))' />

    ZHINENGSHURUSHUZIHEYINGWEN:

    ?1'text' onkeyup='value=value.replace(/[W]/g,'') ' onbeforepaste='clipboardData.setData('text',clipboardData.getData('text).replace(/[^d]/g,''))' />

    荣耀彩票代理YISHANGNEIRONGJIUSHIBENWENDUIZHENGZEBIAODASHIZUODEXIANGJIE,XIWANGDAJIAXIHUAN

延伸阅读:

About IT165 - 广告服务 - 隐私声明 - 版权申明 - 免责条款 - 网站地图 - 网友投稿 - 联系方式
本站内容来自于互联网,仅供用于网络技术学习,学习中请遵循相关法律法规