zoukankan      html  css  js  c++  java
  • 浅谈JavaScript -- 正则表达式

    什么是正则表达式?

    正则表达式是由一个字符序列形成的搜索模式。可用于文本搜索和文本替换。

    语法:/正则表达式主体/修饰符(可选)

    var patt=new RegExp(pattern,modifiers);
    
    或者更简单的方式:
    
    var patt=/pattern/modifiers;
    

    正则表达式修饰符

    修饰符 描述
    i 执行对大小写不敏感的匹配。
    g 行全局匹配(查找所有匹配而非在找到第一个匹配后停止)。
    m 执行多行匹配。

    正则表达式模式

    表达式 描述
    [abc] 查找方括号之间的任何字符。
    [^abc] 查找方括号以外的任何字符。
    [0-9] 查找任何从 0 至 9 的数字。
    (x|y) 查找任何以 | 分隔的选项。
    {n,m} 匹配长度。
    . 任何单个字符。
    转义字符。

    量词:

    量词 描述
    n+ 匹配任何包含至少一个 n 的字符串。
    n* 匹配任何包含零个或多个 n 的字符串。
    n? 匹配任何包含零个或一个 n 的字符串。
    ?=n 匹配任何其后紧接指定字符串 n 的字符串。即n前面的。
    ?!n 匹配任何其后没有紧接指定字符串 n 的字符串。
    var str="name=adoctors;age=24;";
    console.log(str.match(/[^=;]+(?==)/g));
    //[ 'name', 'age' ]
    
    console.log(str.match(/[^=;]+(?!=)/g));
    //[ 'nam', 'adoctors', 'ag', '24' ]
    

    RegExp 对象方法

    方法 描述
    test 检索字符串中指定的值。返回 true 或 false。
    exec 检索字符串中指定的值。返回找到的值,并确定其位置。没有匹配到返回null。

    test方法使用g时,两侧使用时,会出现lastIndex问题。

    var str='hello',patt=/e/g;
    
    function fun(){
    	console.log(patt.lastIndex);
    	console.log(patt.test(str));
    }
    
    fun();  //0,true
    fun();	//2,false
    fun();	//0,true
    fun();	//2,false
    
    //解决方案:
    1、不用g
    2、在第二次使用前设置patt.lastIndex=0;即可。
    

    支持正则表达式的string对象的方法

    方法 描述
    match 检索字符串中指定的值。找到一个或多个匹配到的结果,并输出一个数组。没有匹配到返回null。
    replace 用指定的值替换对应的值。
    split 将字符串分割成数组。

    例:在一段文本的每个行首跟行尾加上p标签

    var str='hello adoctors!';
    var patt=/o/gi;
    console.log(str.match(patt));       //[ 'o', 'o', 'o' ]
    console.log(str.split(patt));       //[ 'hell', ' ad', 'ct', 'rs!' ]
    //split第二个参数设置返回的数组的最大长度
    console.log(str.split(patt,2));       //[ 'hell', ' ad']
    
    
    var strs=`
    aaaa
    
    
    bbbbbbbbbbbb
    
    cccccc
    `;
    var strs2=str.replace(/^/gm,'<p>').replace(/$/gm,'</p>');
    
    //replace方法第二个参数可以是函数
    let str3=str.replace(patt,function($0,$1,$2){
        console.log($0) //匹配到的元素      o,o,o
        console.log($1) //本次匹配到元素对应的位置  //4,8,11
        console.log($2) //要匹配的目标字符串    //hello adoctors!,hello adoctors!,hello adoctors!
        return 2;
    })
    console.log(str3)   //hell2 ad2ct2rs!
    console.log(str)   //hello adoctors!
    

    常用的正则表达式:

    26个大写字母    [A-Z]
    26个小写字母    [a-z]
    0-9数字         [0-9]   或d    当字母为大写是为非:W非数字
    空白字符        s                                  S非空
    包括大小写字母,下划线,0-9 [A-Za-z0-9_]  或w
    匹配中文:[u4e00-u9fa5] //中文ACALL码的范围
    行首行尾空格:^s*|s*$ //首行出现任意个空格或者尾行出现任意个空格(任意表示也可以没有空格)
    检测密码强度正则/^.*(?=.{6,})(?=.*d)(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#$%^&*? ]).*$/
    
    
    //去除html标签行内样式
    var style=`<span style="color:red;">hello adoctors!</span>`;
    var pat=/style="(.*)"/g;
    console.log(style.replace(pat,''));     //<span >hello adoctors!</span>
    
  • 相关阅读:
    操作系统8:文件系统
    操作系统7:内存管理
    操作系统6:死锁
    操作系统5:进程同步
    操作系统3:CPU调度
    操作系统2:进程
    操作系统1:操作系统结构
    计算机组成:CPU
    计算机组成:数制与运算
    计算机组成:输入输出系统
  • 原文地址:https://www.cnblogs.com/adoctors/p/9218454.html
Copyright © 2011-2022 走看看