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

    元字符

    var str = "April 15, 2003";
    var regexp = /(w+) (d+), (d+)/i;  //注意"空格"
    var result = regexp.test(str);
    console.log(result);  //true
    

     w+ w表示任意字符字母,+表示至少一个字符,即匹配至少1个字符

    d+  匹配至少1个数字

    i  表示忽略大小写

    小括号表示捕获语法,不过在此例中没有作用

    var str = "April 15, 2003";
    var regexp = /(w+) (d+), (d+)/i;
    var r = str.replace(/(w+) (d+), (d+)/, "$1 $3, $2" );
    console.log(r);//April 2003, 15

    相关参考

    stringObject.replace(regexp/substr,replacement)  //返回值: 一个新字符串

    1. () 小括号,表示匹配后捕获
    2. (?:), 如果小括号里面有?:,表示不捕获,相关知识点是正则表达式中"反向引用" 参考

    匹配之后的"抓取"效果

    match函数

    var reg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,}$/;
    if (!password.match(reg)) {
        $("#Password").next().text("密码请输入8位以上的数字加字母");
        return false;
    }

     匹配URL中指定参数的值,如这里的"type"的值,正则表达式如下

    //http://localhost/Login/LoginPage?type=776E1102AD1E620D
    var name="type";
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //(^|&)type=([^&]*)(&|$)
    var r = window.location.search.substr(1).match(reg); 

     Chrom中的调试方法  console.log(r);

    1. (^|&):匹配起始位置(字符串开头位置)或者&字符,如果其后还有正则,那么必须出现在字符串开始或&字符之后
    2. RegExp 是javascript中的一个内置对象。为正则表达式。
    3. match()返回值是一个数组,我们这里要抓取值记在第2个括号内,数组索引正好也是2,即r[2]

    javaScript prototype实例与正则表达式联合应用

    //return (new Date(data)).Format("yyyy-MM-dd hh:mm:ss"); 
    Date.prototype.Format = function (fmt) { 
        var o = {
            "M+": this.getMonth() + 1,
            //月份
            "d+": this.getDate(),
            //日
            "h+": this.getHours(),
            //小时
            "m+": this.getMinutes(),
            //分
            "s+": this.getSeconds(),
            //秒
            "q+": Math.floor((this.getMonth() + 3) / 3),
            //季度
            "S": this.getMilliseconds() //毫秒
        };
        if (/(y+)/.test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        }
        for (var k in o) {
            if (new RegExp("(" + k + ")").test(fmt)) {
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            }
        }
        return fmt;
    }

    1.RegExp.$1是RegExp的一个属性,指的是与正则表达式匹配的第一个 子匹配(以括号为标志)字符串,以此类推,RegExp.$2,RegExp.$3,..RegExp.$99总共可以有99个匹配

    var r= /^(d{4})-(d{1,2})-(d{1,2})$/; //正则表达式 匹配出生日期(简单匹配)     
    r.exec('1985-10-15');
    s1=RegExp.$1;
    s2=RegExp.$2;
    s3=RegExp.$3;
    alert(s1+" "+s2+" "+s3)//结果为1985 10 15

     利用正则表达式自定义trim函数

    !String.prototype.trim && (String.prototype.trim = function() {
        return this.replace(/^s+|s+$/g, '');
    });
    
    var testStr = '  superMam ';
    var answerStr = testStr.trim();
    console.log(testStr);
    console.log(answerStr);
  • 相关阅读:
    leetcode 18 4Sum
    leetcode 71 Simplify Path
    leetcode 10 Regular Expression Matching
    leetcode 30 Substring with Concatenation of All Words
    leetcode 355 Design Twitte
    leetcode LRU Cache
    leetcode 3Sum
    leetcode Letter Combinations of a Phone Number
    leetcode Remove Nth Node From End of List
    leetcode Valid Parentheses
  • 原文地址:https://www.cnblogs.com/zhuji/p/7149629.html
Copyright © 2011-2022 走看看