zoukankan      html  css  js  c++  java
  • 正则表达式

    1.可处理正则表达式的方法有:regexp.exec、regexp.test、string.match、string.replace、string.search和string.split.

    2.在正则表达式中,元字符都必须转义元字符有   ( [ { ^ $ | ) ? * + . ] },转义符号为“”

    3.正则表达式能设置3个标识:g(全局的)、i(大小写不敏感)、m(多行)

    4.转义字符:d  数字,相当于[0-9]      D 非数字,相当于[^0-9]

                    w  字母、数字、下划线,相当于[a-zA-Z0-9_]   W 相当于相当于[^a-zA-Z0-9_]

                    s  空白字符         S  非空白字符

    5.量词

         x?  匹配0个或1个x,相当于x {0,1}

         x*   匹配0个或任意多个x,相当于x{0,}

         x+   匹配一个或多个x,相当于x{1,}

         x{m,n}   匹配最少m个,最多n个x,相当于x{m,n}

         x{m}   匹配m个x

         (xyz)+   匹配最好一个(xyz)

    5. /^/  表示从一开始就开始匹配 , /$/  表示一直匹配到行尾,/^$/表示从行首匹配到行尾

    6.举例

        1)输入一个字符串,字符串中仅出现“字母、数字、下划线、.、-”时返回true,否则返回false

    1    function judgeStr(str){
    2        var pattern = /^[w.-]+$/;
    3        return pattern.test(str);  //用正则表达式调用test()函数时,正则表达式后面不要加g
    4    }
    5    console.log(judgeStr("hello-world"));  //true
    6    console.log(judgeStr("hello*world"));  //false

        2)去掉字符串前后的空格  

    1   String.prototype.trim =  String.prototype.trim || function(){
    2       var pattern = /^s+|s+$/g;
    3       //replace的第一个参数为字符串时,只能替换第一个子字符串,
    4       //为正则表达式,且指定全局(g)标志,才能替换所有子字符串
    5       return this.replace(pattern,"");
    6   };
    7   console.log("  fff  ".trim().length);  //3
  • 相关阅读:
    angular.isDefined()
    angular.isDate()
    angular.isArray()
    .NET中栈和堆的比较
    SQL Server 2012配置Always On可用性组
    一分钟了解负载均衡的一切
    C# 线程并发锁
    获取Http请求参数
    什么是WCF
    Bitmap算法应用
  • 原文地址:https://www.cnblogs.com/webliu/p/4665804.html
Copyright © 2011-2022 走看看