正则

发布时间 2023-07-03 09:28:57作者: miriz
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

</body>
</html>
<script>
    //匹配字母
    const reg1=/[a-zA-Z]/
    //匹配数字
    const reg2=/\d/
    //匹配非数字
    const reg3=/\D/
    //空格
    const reg4=/\s/
    //字母,数字,下划线
    const reg5=/\w/
    //特殊字符
    const reg6=/[!@#$%^&*]/
    //非字母匹配
    const reg7=/[^a-zA-Z]/
    // **********************
    //text()检测一个字符串是否与正则表达式匹配,返回布尔值
    const reg8=/hello/
    const str1='hello,word!'
    console.log(reg8.text(str1))
    //
    console.log(str1.match(reg8))

    const reg9=/world/
    console.log(str1.replace(reg9,'1111'))
    console.log(str1.split(/\s/))

    /************************位置********************************/
    const str2 = 'hello world'
    const str3 = 'AAAhello world'
    const reg10 = /hello/
    const reg11 = /^hello/
    console.log(reg10.test(str2),reg10.test(str3))
    console.log(reg11.test(str2),reg11.test(str3))
    //举例:匹配手机号
    //const phoneNum='13312341234'
    const phoneNum = '13312341234dddd'
    //const phoneNum='aaaa13312341234'
    const phoneNum = /^1\d{10}$/
    console.log(phoneNum.test(phoneNum))


    //如上.$代表结束位置
    //  \b 代表匹配单词边界
    //  \B非单词边界
    const reg12 = /\bworld\b/
    console.log(reg12.test(str2))
    const str4='helloworld'
    console.log(reg12.test(str4))


    /************量词************************************/
    /*

    *匹配前一个字符出现0次或多次
    /ab*c/ 匹配ac,abc,abbbc等

    +匹配前你一个字符出现1次或多次
    /ab+c/ 匹配abc,abbbc,不匹配ac

    ?匹配前一个字符出现0次或1次
    /ab?c/ 匹配ac,abc,不匹配abbbc

    {n}匹配前一个字符刚好出现n次,多了也不行,少了也不行

    {n,}匹配前一个字符,至少n次

    {n,m}出现n~m次

     */

    //贪婪匹配和惰性匹配
    const string='aaaaaa'
    const greedyPattern=/a+/    //贪婪匹配,尽可能足够多
    const lazyPattern=/a+?/     //惰性匹配,一只一个

    console.log(string.match(greedyPattern))
    console.log(string.match(lazyPattern))

    /*********************字符类别**************************************/
    /*    [abc]匹配的是a或b或c
    [a-z]
    [A-Z]
    [a-zA-Z]
    [0-9]
    [0-9a-fA-F]  //匹配十六进制字符
    [^abc]    //非a非b非c
*/
    /*************************分组************************************/
    /*    (abc)匹配abc字符串
    (ab)+ 匹配连续出现的ab字符串,如ab abab,ababab等
    (a|b) //等同于[ab]
        (abc|def)*/
    /******************************捕获*******************************************/
    /*捕获小括号的嵌套
        (a(b)c) 分组的里面又有一个分组,就是捕获
     */

    /*********************修饰符*****************************************/
            //1. i不区分大小写
    const reg13=/hello/ig
    console.log(reg13.test("Hello"))
    //2. 全局匹配 g 是global, 全球,全局的,所有都会被匹配
    console.log("Hello,hello,Hello",match(reg13))
    //3.m代表多行匹配
    const  pattern = /^hello/im
    console.log("Hollo\nhellohello",match(pattern)) // 输出:["Hello","hello"]
//     匹配由数字和字母组成的字符串,且长度为5到10个字符之间。
// 匹配一个有效的邮箱地址。
// 匹配一个日期,格式为YYYY-MM-DD,例如2023-01-01。
/^\d{4}-\d{2}-\d{2}$/
    // 匹配一个手机号码,格式为11位数字,以1开头。
/^1\d{10}$/
    // 匹配一个由字母组成的字符串,第一个字母必须大写。
/^[A-Z][a-zA-Z]*$/
    // 匹配一个包含特殊字符(例如@、#、$)的字符串。
/[!@#$%^&*]/
    // 匹配一个URL,以http或https开头,并以.com结尾。
/^(http|https):\/\/[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/
    // 匹配一个HTML标签,例如<div>或<p>。
/^<([a-zA-Z]+\d?)([^<>]+)*(?:>(.*)<\/\1>/\s*\/?>)$/

</script>