正则表达式(js篇)

  本文主要针对js中正则表达式的实践操作,来让大家对正则表达式有一个入门清晰的了解。

  正则表达式推荐学习网址:http://www.runoob.com/regexp/regexp-tutorial.html

  一、正则表达式

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>正则表达式测试</title>
    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
    <script type="text/javascript">
        //普通匹配符
        var str = "abcdefad";
        var regex = /b/;
        //正则标记符  i:不区分大小写  g:全局匹配  m:换行匹配
        str = "HELLO JAVA,hello java";
        regex = /JAVA/gi;
        // \d:0-9的数字    \w字母数字下划线   .除换行外的任意一个字符
        str = "HELL-&*&%&%\nO121";
        regex = /\w/g;
        //能够自定义规则的匹配符 []
        str = "heelo.jpg";
        regex = /\.(png|jpe?g|ifgh)/;
        //自定义个数的符号{}
        str = "1382745321";
        regex = /1[3589]\d*/;
        //完整匹配 ^ $
        str = "111111111111111";
        regex = /^1[3589]\d{9}$/;
        //匹配中文字符
        str = "1哈哈898*(*(*";
        regex = /[\u4e00-\u9fa5]/g;
        //匹配3-5中其中一个
        str = "23456789";
        regex = /[3-5]/g;
        //匹配邮箱
        str = "[email protected]";
        regex = /^\d+@qq\.com$/;
        //匹配18位身份证号码
        str = "42102419890615213x";
        regex = /^(?:\d{6})(?:\d{4})(?:\d{2})(?:\d{2})(?:\d{3})(?:[0-9]|X)$/i;
        
        var r = str.match(regex);
        console.info(r);
    </script>
  </head>
  
  <body>
  </body>
</html>

猜你喜欢

转载自www.cnblogs.com/ouyy/p/study_day.html