首页 > 其他分享 >前端学习-正则表达式

前端学习-正则表达式

时间:2024-01-23 22:36:29浏览次数:27  
标签:匹配 log exec 正则表达式 前端 学习 let console match

学习MDN-正则表达式章
学习MDN-正则表达式章-这个链接也可以

什么是正则表达式

要点:

  • 用于匹配字符串中字符组合的模式
  • js中,正则表达式也是对象
  • 用于 RegExp 的 exec 和 test 方法,以及 String 的 match、matchAll、replace、search 和 split 方法

g i m 分别是什么意思

参考博客

/i (忽略大小写)
/g (全文查找出现的所有匹配字符)
/m (多行查找)
/gi(全文查找、忽略大小写)
/ig(全文查找、忽略大小写)
/d(生成子串匹配的索引)
/s(允许 . 匹配换行符)
/u(“Unicode”;将模式视为 Unicode 码位序列)
/v(升级 u 模式,提供更多 Unicode 码特性)
/y(执行“粘性(sticky)”搜索,从目标字符串的当前位置开始匹配)

方法

RegExp.prototype.exec()

在一个指定字符串中执行一个搜索匹配。返回一个结果数组或 null
要点1:

  • 当regex设置了全局匹配/g时,regex.exec(str) 每次匹配得到一个字符串,并且将正则表达式的lastIndex置为匹配到的字符串对应的索引位置,第二次执行regex.exec(str),会从正则表达式的lastIndex开始匹配,如下例
  • 如果匹配失败,exec() 方法返回 null,并将正则表达式的 lastIndex 重置为 0
  • 注意,即使再次查找的字符串不是原查找字符串时,lastIndex 也不会被重置,它依旧会从记录的 lastIndex 开始
<script>
    const regex = /foo*/g;
    const str = 'table fooootball, fosball';

    let myArray = [];
    while ((myArray = regex.exec(str)) !== null) {
        let msg = `Found ${myArray[0]}. `;
        msg += `Next match starts at ${regex.lastIndex}`;
        console.log(msg);
    }
    // Found foooo. Next match starts at 11
    // Found fo. Next match starts at 20
</script>
  • 虽然 exec() 本身非常强大而又有效,但它通常不能最清楚地表示调用的目的。因此在不同场景下,可使用test(),match(),matchAll(),search()方法代替

要点2:

  • 如果匹配成功,exec() 方法返回一个数组,并更新正则表达式对象的 lastIndex 属性
  • 完全匹配成功的文本将作为返回数组的第一项,从第二项起,后续每项都对应一个匹配的捕获组
  • 如下例:第一个Brown是匹配到的文本,第二个Brown是一个匹配的捕获组
const re4 = /(?<color>brown)/gi;
console.log(re4.exec("The Quick Brown Fox Jumps Over The Lazy Dog")); // [ "Brown", "Brown" ]

简单模式

/abc/可以匹配到字符串中第一个"abc"
如下例

console.log(('abc ssijah aaaabcc sad').replace(/abc/,'hhh')); //结果:hhh ssijah aaaabcc sad

使用特殊字符

断言(Assertions)

断言详细知识点
解析示例

<script>
        let orangeNotLemon = "Do you want to have an orange? Yes, I do not want to have a lemon!";
                                                                                                                                    
        let selectNotLemonRegex1 = /[^?!]/gi; // 范围-匹配除了'?','!'以外的所有字符
        let selectNotLemonRegex2 = /have(?! a lemon)/gi; // 先行否定断言-匹配后面没有' a lemon'的'have'
        let selectNotLemonRegex3 = /have(?! a lemon)[^?!]/gi; // selectNotLemonRegex1中跟随'have'仅有字符' ',因此匹配到'have '
        let selectNotLemonRegex4 = /[?!]/gi; // 范围-匹配'?','!'
        let selectNotLemonRegex5 = /[^?!]+have(?! a lemon)[^?!]/gi; // +指多次匹配前面括号内的表达式(此处为[^?!]),因此[^?!]+匹配到'have '前的所有字符'Do you want to ',返回'Do you want to have '
        let selectNotLemonRegex6 = /[^?!]+have(?! a lemon)[^?!]+[?!]/gi; // have(?! a lemon)[^?!]匹配到'have an orange',

        console.log(orangeNotLemon.match(selectNotLemonRegex1)); // [ 'D', 'o', ' ', 'y', 'o', 'u', ' ', 'w', 'a', 'n', 't', ' ', 't', 'o', ' ', 'h', 'a', 'v', 'e', ' ', 'a', 'n', ' ', 'o', 'r', 'a', 'n', 'g', 'e', ' ', 'Y', 'e', 's', ',', ' ', 'I', ' ', 'd', 'o', ' ', 'n', 'o', 't', ' ', 'w', 'a', 'n', 't', ' ', 't', 'o', ' ', 'h', 'a', 'v', 'e', ' ', 'a', ' ', 'l', 'e', 'm', 'o', 'n']
        console.log(orangeNotLemon.match(selectNotLemonRegex2)); // [ 'have' ]
        console.log(orangeNotLemon.match(selectNotLemonRegex3)); // [ 'have ' ]
        console.log(orangeNotLemon.match(selectNotLemonRegex4)); // [ '?', '!' ]
        console.log(orangeNotLemon.match(selectNotLemonRegex5)); // [ 'Do you want to have ' ]
        console.log(orangeNotLemon.match(selectNotLemonRegex6)); // [ 'Do you want to have an orange?' ]
    </script>

字符类(Character Classes)

字符类详细知识点

n*

匹配前一个表达式 0 次或多次。等价于 {0,}。
例如,/bo*/ 会匹配 "A ghost boooooed" 中的 'booooo' 和 "A bird warbled" 中的 'b',但是在 "A goat grunted" 中不会匹配任何内容。

示例
<script>
    const aliceExcerpt = "I'm sure I'm not Ada,' she said, 'for her hair goes in such long ringlets, and mine doesn't go in ringlets at all.";
    const regexpWordStartingWithA = /\b[aA]\w+/g;
    // \b 表示边界(即不要在单词中间开始匹配)
    // [aA] 表示字母 a 或 A
    // \w+ 表示任何*拉丁字母*字符,多次

    console.table(aliceExcerpt.match(regexpWordStartingWithA));
    // ['Ada', 'and', 'at', 'all']

</script>

组和范围(Groups and ranges)

组和范围详细知识点

捕获组(x)
  • 捕获组:匹配 x 并记住匹配项。例如,/(foo)/匹配并记住“foo bar”中的“foo”
  • 会有性能损失
  • 非捕获括号:String.match() String.matchAll()

示例(使用捕获组):

<script>
    let personList = `First_Name: John, Last_Name: Doe
    First_Name: Jane, Last_Name: Smith`;

    let regexpNames = /First_Name: (\w+), Last_Name: (\w+)/gm;
    let match = regexpNames.exec(personList);

    // console.log(personList.match(regexpNames));
    // console.log(match); // ['First_Name: John, Last_Name: Doe', 'John', 'Doe']

    do {
        console.log(`Hello ${match[1]} ${match[2]}`);
    } while ((match = regexpNames.exec(personList)) !== null);
</script>
具名捕获组(?x)

匹配"x"并将其存储在返回的匹配项的 groups 属性中,该属性位于指定的名称下。尖括号 (< 和 >) 用于组名。

<script>
    let users = `姓氏:李,名字:雷
    姓氏:韩,名字:梅梅`;

    let regexpNames = /姓氏:(?<first>.+),名字:(?<last>.+)/gm;
    let match = regexpNames.exec(users);

    do {
    console.log(`Hello ${match.groups.first} ${match.groups.last}`);
    } while ((match = regexpNames.exec(users)) !== null);

    // Hellow 李 雷
    // Hellow 韩 梅梅

</script>

量词(Quantifiers)

贪婪 非贪婪
    <script>
        let text = "I must be getting somewhere near the centre of the earth.";
        // []是范围 [\w ]是匹配字母或空格 [\w ]+匹配所有字母或空格
        let greedyRegexp = /[\w ]+/;
        // [\w ]      a letter of the latin alphabet or a whitespace
        //      +     one or several times

        console.log(text.match(greedyRegexp));
        // "I must be getting somewhere near the centre of the earth"

        let nonGreedyRegexp = /[\w ]+?/; // Notice the question mark
        console.log(text.match(nonGreedyRegexp));
        // "I"

    </script>
设置匹配几次

Unicode 属性转义(Unicode Property Escapes)

一般类别
<script>
    // finding all the letters of a text
    let story = "It's good!";

    // Most explicit form
    // story.match(/\p{General_Category=Letter}/gu);
    console.log(story.match(/\p{General_Category=Letter}/gu)); // ['I', 't', 's', 'g', 'o', 'o', 'd']

    // It is not mandatory to use the property name for General categories
    // story.match(/\p{Letter}/gu);
    console.log(story.match(/\p{Letter}/gu)); // ['I', 't', 's', 'g', 'o', 'o', 'd']

    // This is equivalent (short alias):
    // story.match(/\p{L}/gu);
    console.log(story.match(/\p{L}/gu)); // ['I', 't', 's', 'g', 'o', 'o', 'd']

    // This is also equivalent (conjunction of all the subcategories using short aliases)
    // story.match(/\p{Lu}|\p{Ll}|\p{Lt}|\p{Lm}|\p{Lo}/gu);
    console.log(story.match(/\p{Lu}|\p{Ll}|\p{Lt}|\p{Lm}|\p{Lo}/gu)); // ['I', 't', 's', 'g', 'o', 'o', 'd']

</script>
文字

···

···

Unicode 属性转义 vs. 字符类
  • 字符类 尤其是 \w 或 \d 匹配字母或数字,仅能匹配拉丁文字的字符 (换言之,a 到 z、 A 到 Z 的 \w 和 0 到 9 的 \d)
  • 但Unicode 属性转义包含更多字符,\p{Letter} 或 \p{Number} 将会适用于任何文字
<script>
    // Trying to use ranges to avoid \w limitations:

    const nonEnglishText = "Приключения Алисы в Стране чудес";
    const regexpBMPWord = /([\u0000-\u0019\u0021-\uFFFF])+/gu;
    // BMP goes through U+0000 to U+FFFF but space is U+0020

    console.table(nonEnglishText.match(regexpBMPWord));

    // Using Unicode property escapes instead
    const regexpUPE = /\p{L}+/gu;
    console.table(nonEnglishText.match(regexpUPE));

</script>

标签:匹配,log,exec,正则表达式,前端,学习,let,console,match
From: https://www.cnblogs.com/ayubene/p/17980740

相关文章

  • c++学习由浅入深刷题指南
    新手村任何一个伟大的目标,都有一个微不足道的开始。洛谷的第一个任务勇敢的迈出第一步,了解下语言和洛谷。跟着书本和老师走,不会难的。P1000P1001P1421P1425顺序与分支计算机的智能性开始得以体现,因为计算机能够根据不同的条件选择了。P1422P1085P1089P1909循环!......
  • 卷积神经网络学习笔记
    全连接神经网络的结构全连接神经网络的整体结构可以简化为智能函数\(y=f_θ(x)\)输入和输出层一般为数据矩阵全连接网络的单元结构神经网络的思路:从单元到整体一个单元的结构:\(X_1,X_2,X_3...\)是很多矩阵,然后这些矩阵分别乘上对应的权重矩阵,再加上偏置矩阵b,输......
  • Binary tree traversal-- beadth-first and depth-first【1月23日学习笔记】
    点击查看代码//Binarytreetraversal--beadth-firstanddepth-first#include<iostream>#include<queue>//STLusingnamespacestd;structnode{intdata;node*left,*right;};node*getnewnode(intx){node*temp=newnode;temp-&......
  • Java学习日记 Day9 今天小摆烂,明天加油~
    Spring框架:①IOC:简单说就是把创建对象的任务交给了配置文件,降低了代码耦合。②DI:IOC实现创建对象的任务后DI技术可以为在配置文件中为对象赋值。③代理模式:代替一个对象完成任务,分为静态代理和动态代理,动态代理有JDK和CGLIB两种,前者只可以对接口中的方法实现增强,而后者也可以对......
  • 手型机器人、灵巧手机器人:交互感知-行为提取-意图理解-技能生成-运动映射-灵巧操作”
    灵巧手机器人,灵巧精准操作的手型机器人,最有名的应该就是Google的Deepmind推出的可以玩魔方的手型机器人了,如下图:相关资料:https://baijiahao.baidu.com/s?id=1647601517185392390&wfr=spider&for=pchttps://m.thepaper.cn/baijiahao_4728005地址:http://www.ia.cas.cn/kygz......
  • 今天学习基础知识,抽象类和接口
    今天学习基础知识,抽象类和接口,之前这块学的不太好没有太了解。抽象类与接⼝1.抽象类抽象类和抽象⽅法都使⽤abstract关键字进⾏声明。如果⼀个类中包含抽象⽅法,那么这个类必须声明为抽象类。抽象类和普通类最⼤的区别是,抽象类不能被实例化,只能被继承。privateInnerClas......
  • C#学习笔记-类、对象、类成员
    类(class)  在类与名称空间简单学习了类的概念。程序世界中的类是对现实世界的事物进行抽象的结果,类定义了事物的特点、行为,在一定程度上反映现实事物的样子。但类又舍弃了一些不必要的内容(在程序中没有涉及),是现实事物的模型。建模是一个由表及里的过程,向外提供了易于使用的接口,......
  • 深度学习3D目标检测综述文献
    本文主要参考以下几篇综述文献,对3D目标检测相关方法有个大致的了解,3D目标检测输出结果包含7维,\([x_c,y_c,z_c,l,w,h,\theta]\),其中\([x_c,y_c,z_c]\)表示框的中心点位置,\([l,w,h]\)分别表示框的长宽高,\(\theta\)表示框的角度。以下几篇参考文献中第一篇将点云目标检测分......
  • 发育学习:机器人智能发育算法
    看到了一个机器人领域的新概念,叫做“发育学习”,比较陌生,于是查了下资料,这里也是简单谈下个人的理解。在机器学习领域有个稍微相近的概念,叫做“终身学习”,也就是使用同一个模型对于相同类型的不同学习任务进行分别学习,不同任务时序出现,比如,先出现A任务,然后出现B任务,然后出现C任务,......
  • Binary tree traversal-- level-order traversal using queue【1月23日学习笔记】
    点击查看代码//Binarytreetraversal--level-ordertraversalusingqueue#include<iostream>#include<queue>//STLusingnamespacestd;structnode{intdata;node*left,*right;};node*getnewnode(intx){node*temp=newnode;t......