zoukankan      html  css  js  c++  java
  • JavaScript正则

    使用new RegExp('规则','修饰符(匹配模式)')或者/规则/修饰符(匹配模式)来创建一个正则表达式

    const reg = /box/; // 检查一个字符串中是否包含字符串'box'
    const str = 'big box';
    

    正则的方法

    test方法 正则表达式.test(字符串) 返回值为Boolean
    const reg = /box/;
    const str = 'big box';
    
    console.log(reg.test(str)); // => true
    
    exec方法 正则表达式.exec(字符串) 返回一个伪数组,其中存放匹配的结果。如果未找到匹配,则返回值为 null。
    const reg = /box/;
    const str = 'big box';
    
    const res = reg.exec(str);
    console.log(res); // => ["box", index: 4, input: "big box", groups: undefined]
    
    console.log(Array.from(res)); // => ["box"]
    
    source 返回正则表达式的正文
    const reg = /box/g;
    
    console.log(reg.source) // => box
    
    flags 返回正则表达式的修饰符
    const reg = /box/gi;
    
    console.log(reg.flags) // => gi
    
    lastIndex 该属性用于规定下次匹配的起始位置, 该属性只有设置标志 g 才能使用。

    正则的匹配模式

    i 表示忽略大小写

    g 为全局匹配模式(查找所有匹配而非在找到第一个匹配后停止)

    u ES2015新增,含义为“Unicode模式”,用来正确处理大于uFFFF的Unicode 字符。也就是说,会正确处理四个字节的 UTF-16 编码。

    y ES2015 还为正则表达式添加了y修饰符,叫做“粘连”(sticky)修饰符。y修饰符的作用与g修饰符类似,也是全局匹配,后一次匹配都从上一次匹配成功的下一个位置开始。不同之处在于,g修饰符只要剩余位置中存在匹配就可,而y修饰符确保匹配必须从剩余的第一个位置开始,这也就是“粘连”的涵义。

    s ES2018 使得.可以匹配任意单个字符

    *u,y,s详见阮大神的ES6指南

    const reg1 = /b/;
    const reg2 = /b/i;
    
    const str = 'newBox';
    
    console.log(reg1.test(str)); // => false
    console.log(reg2.test(str)); // => true
    
    /foo.bar/s.test('foo
    bar') // => true
    

    正则的语法规则

    1. 使用|来验证字符串中是否有一个或多个字符
    const reg = /a|b/; // 是否包含a或b
    const str = 'watch';
    console.log(reg.test(str)); // => true
    
    1. []中的内容也表示或的关系(查找集合内的指定字符)
    const reg1 = /[ab]/; // 是否包含a或b
    const reg2 = /[d-f]/; // 是否包含小写字母d到小写字母f
    const reg3 = /[A-Z]/; // 是否包含大写字母A到大写字母Z
    const reg4 = /[A-z]/; // 不区分大小写
    const reg5 = /[0-9]/; // 任意数数字
    const str = 'watch';
    
    console.log(reg1.test(str)); // => true
    console.log(reg2.test(str)); // => false
    
    1. [^ ] 表示取反,在[]中除了^后面的内容都满足
    const reg1 = /[^box]/; // 除了'box' 之外的都满足
    const reg2 = /[^0-9]/; // 除了数字
    const srt1 = 'new box';
    const srt2 = 'box';
    
    console.log(reg1.test(srt1)); // => true
    console.log(reg1.test(srt2)); // => false
    
    1. 量词 {数量} 通过量词可以设置一个内容出现的次数
    const phone = '15207176526';
    const reg1 = /[0-9]{11}/; // 任意数字出现11次
    const reg2 = /[0-9]{3, 9}/; // 任意数字出现3到9次
    
    console.log(reg1.test(phone)); // => true
    console.log(reg2.test(phone)); // => false
    

    {}中的量词

    n+ 一个或多个
    n* 零个或多个
    n? 零个或一个
    1. '^' 以某个字符开头
    const reg = /^h/; // 以h开头
    const srt = 'https://'
    
    console.log(reg.test(srt)); // => true
    
    1. '$' 以某个字符结尾
    const reg = /(c(om|n))$/; // 以 com 或者 cn 结尾
    const srt = 'com'
    
    console.log(reg.test(srt)); // => true
    
    1. 元字符
    . 任意字符
    . .
    \
    d 任意数字相当于[0-9]
    D 除了0到9
    w 0到9或任意大小写字母
    W 除了字母、数字、下划线的任意字符
    s 任意不可见字符, 包括空格、制表符、换行符等
    S 除了不可见字符, 包括空格、制表符、换行符等任意字符
     匹配单词的边界
    制表符
    匹配换行

    字符串和正则之间的方法

    split可以将字符串拆分为数组

    const str = '1a2b3c4d5e6';
    
    console.log(str.split(/[A-z]/)); // => ["1", "2", "3", "4", "5", "6"]
    

    search检索字符串中是否含有指定字符,返回第一个符合条件的字符索引,找不到则返回-1

    const str = 'I have 9 dollar';
    
    console.log(str.search(/[0-9]/));
    

    match根据正则表达式讲符合条件的内容提取出来

    const str = '6f12ffP122d';
    const srtArr = str.match(/[A-z]/g); // =>  ["f", "f", "f", "P", "d"]
    
    console.log(srtArr.join('')); // => 'fffPd' 
    

    replace可以将字符串按照规则替换为指定内容, 返回新的内容

    const srt = 'Dogsbody';
    
    console.log(srt.replace(/sb/i, '**')) // => Dog**ody
    

    split可以将字符串按照规则分割成字符串数组

    const str = "h1e2l3l4o";
    
    console.log(str.split(/[0-9]/g)); // =>  ["h", "e", "l", "l", "o"];
    

    matchAll ES2020新增,可以一次性取出所有匹配。不过,它返回的是一个遍历器(Iterator),而不是数组。

    const string = 'test1test2test3';
    const regex = /t(e)(st(d?))/g;
    
    for (const match of string.matchAll(regex)) {
      console.log(match);
    }
    // =>  
    /*
    ["test1", "e", "st1", "1", index: 0, input: "test1test2test3"]
    ["test2", "e", "st2", "2", index: 5, input: "test1test2test3"]
    ["test3", "e", "st3", "3", index: 10, input: "test1test2test3"]
    */
    
    为之则易,不为则难。
  • 相关阅读:
    Python unittest单元测试框架总结
    RabbitMQ集群搭建
    mysql之mysqldump——备份与还原
    新版本Ubuntu本地提权漏洞复现
    Flash 零日漏洞复现(CVE-2018-4878)
    申论之道
    上海失业金
    C# GUID有什么用?
    C#通过接口或者父类可以调用子类的方法或者属性吗?
    C# 按逗号分隔字符串&强制类型转换string转double
  • 原文地址:https://www.cnblogs.com/coderDemo/p/13293982.html
Copyright © 2011-2022 走看看