• 正则表达式学习


    应用

    1. 输入校验
    2. AST 抽象语法树
    3. liunx命令

    创建一个正则表达式

    1. 使用正则表达式字面量
    const regex = /^d{11}&/
    
    1. 使用构造函数
    const regex = new RegExp(/^d{11}&/, "gi")
    

    如何判断正则表达式的类型?

    1. typeof
    2. instanceof RegExp

    基础知识

    元字符 必记

    • . 匹配 以外的任意字符
    • w 字母数字下划线 (a-zA-Z0-9_)
    • W 非字母数字下划线
    • d 数字 (0-9)
    • D 非数字
    • s 任意不可见字符 如空格 制表符 换行符
    • S 任意可见字符
    • ^ 开始符 // 字符集里第一位代表 非
    • $ 结束符

    量词——关于数量的词

    • {n} n次
    • {n, } 至少n次
    • {n,m} n到m次
    • * 重复任意次 相当于{0,}
    • ? 0-1次 {0,1}
    • + 至少1次 {1,}

    分支和字符集合

    • (ax|by|cz) 分支
    • [abc] === [a-c] 字符集合
    • [^abc] === [^a-c] 字符集合
    • a-c 代表 abc
    • [] === [x00-xff] x代表十六进制
    • [] === [u0000-u00ff] u unicode编码
    • 中文字符[u4e00-u9fa5] unicode编码起始-结束

    修饰符

    1. g global
    var str = 'ha ha ha';
    var result = str.replace(/ha/, 'Hello');
    var gResult = str.replace(/ha/g, 'Hello');
    console.log(str);    // "ha ha ha" 
    console.log(result); // "Hello ha ha"
    console.log(gresult); // "Hello Hello Hello"
    
    1. i ignoreCase
    2. m multiline 多行
    3. y sticky [es2016]
    4. u unicode [es2018]
    5. s dotAll [es2018]d

    高级

    转义

    • 哪些字符需要转义? 一般产生歧义的字符都需要转义,常见转义符号如下
    1. .
    2. ^
    3. $
    4. *
    5. +
    6. {}
    7. ()

    分组&引用

    • /(d{4})-(d{2})-(d{2})/ 分组,分三组

    • /(d{4})-(d{2})-2/
      2分组,对前两组的引用 内容一样非表达式一样

      const result = /(d{4})-(d{2})-(d{2})/.exec('1111-22-33')
      result[0] // '1111-22-33' 
      result[1] // '1111'
      result[2] // '22'
      result[3] // '33'
      
    
    • 具名分组 // [es2018]
    const res = `1111-22-33`.match(/(?<year>d{4})-(?<month>d{2})-(?<day>d{2})/)
    console.log(res)
    {
    0: '1111-22-33',
    1: '1111',
    2: '22',
    3: '33',
        groups: {
            year: 1111,
            month: 22
            day: 33
        }
    }
    console.log(res.groups) // {year: "1111", month: "22", day: "33"}
    
    • replace
    `1111-22-22`.replace(/(d{4})-(d{2})-(d{2})/, `$1/$2/$3`)
    // 1111/22/33
    
    • RegExp.$[_1-9]
    • ?: 分组不引用
    •  单词边界

    方法

    • test 测试是否匹配的RegExp方法,返回true或false
    • exec RegExp方法,返回数组或null
    • match 字符串方法,返回数组或null
    • replace 字符串方法,返回替换后的结果字符串,不更改原字符串

    lookaround assertions (零宽断言/环视)

    • 根据方向 分为 lookahead和lookbehind
    • 根据判定原则, 分为肯定和否定
    1. 正向 肯定 ?=pattern 如果pattern匹配 整个表达式匹配

    2. 正向 否定 ?!pattern 如果pattern匹配 整个表达式不匹配

    3. 反向 肯定 [es2018] ?<=pattern

    4. 反向 否定 [es2018] ?<!pattern

    惰性 & 贪婪

    1. 贪婪模式 在匹配成功的前提下,尽可能多的去匹配。 匹配优先量词
    2. 惰性模式 在匹配成功的前提下,尽可能少的去匹配。 匹配忽略优先量词

    参考:

  • 相关阅读:
    Linux报错排解
    linux中wget 、apt-get、yum rpm区别
    Java NIO系列教程
    Java NIO系列教程(七) FileChannel
    使用一条sql查询多个表中的记录数
    ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
    java.security.NoSuchAlgorithmException: SHA1PRNG SecureRandom not available
    javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed
    Oracle查询锁表和解锁
    Data source rejected establishment of connection, message from server: "Too many connections"
  • 原文地址:https://www.cnblogs.com/BillyQin/p/10045035.html
Copyright © 2020-2023  润新知