• JS正则表达式从入门到入土(10)—— 字符串对象方法


    字符串对象方法

    search方法

    String.prototype.search(reg)

    search方法用于检索字符串中指定的子字符串,或检索与正则表达式相匹配的子字符串,方法返回第一个匹配结果的index,查找不到则返回-1

    'a1b2c3d1'.search('1')		// 1
    
    'a1b2c3d1'.search('10')		// -1
    
    'a1b2c3d1'.search(/1/)		// 1
    
    'a1b2c3d1'.search(/1/g)		// 1
    
    'a1b2c3d1'.search(/1/g)		// 1
    
    'a1b2c3d1'.search(1)		// 1
    

    tips:

    1.search方法不执行全局匹配,它将忽略标志g,并且总是从字符串的开始进行检索,因此,它不会产生类似于test方法的问题

    2.不输入正则表达式则search方法将会自动将其转为正则表达式

    match方法

    String.prototype.match(reg)

    match方法将检索字符串,以找到一个或多个与reg匹配的文本,reg是否具有标志g对结果影响很大。

    非全局调用

    如果reg没有标识g,那么match方法就只能在字符串中执行一次匹配,如果没有找到任何匹配的文本,将返回null,否则,它将返回一个数组,其中存放了与它找到的匹配文本有关的信息。

    返回数组的第一个元素存放的是匹配文本,而其余的元素存放的是与正则表达式的子表达式匹配的文本。

    除了常规的数组元素之外,返回的数组还含有2个对象属性:

    • index:声明匹配文本的起始字符在字符串的位置
    • input:声明对stringObject的引用
    let reg = /d(w)d/
    
    let text = '$1a2b3c4e5e'
    
    // 子表达式是 /w/,匹配a
    let result = text.match(reg)	  // ["1a2", "a"]
    
    result.index					// 1
    
    // 不管lastIndex
    result.lastIndex				// 0
    
    result.input					// '$1a2b3c4e5e'
    
    全局调用

    如果regexp具有标志g则match方法将执行全局检索,找到字符串中的所有匹配子字符串。如果没有找到任何匹配的子串,否则,返回一个数组。

    数组元素中存放的是字符串中所有的匹配子串,而且也没有index属性或input属性。

    let reg = /d(w)d/g
    
    let text = '$1a2b3c4e5e'
    
    let result = text.match(reg)		// ["1a2", "3c4"]
    
    result.index					   // undefined
    
    result.input					   // undefined
    
    result.lastIndex				   // 0
    

    split方法

    String.prototype.split(reg)

    我们经常使用split方法将字符串分割为字符数组:

    'a, b, c, d'.split(',')			// ["a", "b", "c", "d"]
    

    在一些复杂的分割情况下我们可以使用正则表达式解决:

    'a, b, c, d'.split(/,/)			// ["a", "b", "c", "d"]
    
    'a1b2c3d'.split(/d/)			// ["a", "b", "c", "d"]
    

    replace方法

    replace方法有三种形态:

    1.String.prototype.replace(str, replaceStr)

    2.String.prototype.replace(reg, replaceStr)

    'a1b1c1'.replace('1', 2)		// 'a2b1c1'
    
    'a1b1c1'.replace(/1/g, 2)		// 'a2b2c2'
    

    3.String.prototype.replace(reg, function)

    function会在每次匹配替换的时候调用,有四个参数

    1.匹配字符串

    2.正则表达式分组内容,没有分组则没有该参数

    3.匹配项在字符串中的index

    4.原字符串

    'a1b2c3d4e5'.replace(/d/g, (match, index, origin) => {
        console.log(index)
        return parseInt(match) + 1
    })
    
    // 1 3 5 7 9
    // 'a2b3c4d5e6'
    
    
    'a1b2c3d4e5'.replace(/(d)(w)(d)/g, (match, group1, group2, group3, index, origin) => {
        console.log(match)
        return group1 + group3
    })
    
    // '1b2' '3d4'
    // ''a12c34e5		=> 去除了第二个分组w匹配到的b和d
    
  • 相关阅读:
    别人好的资源路径
    是否为微信浏览器,苹果安卓判断
    iframe滚动条置顶
    hadoop之MapReduce WordCount分析
    CentOS FTP服务器权限控制
    linux之sed用法
    hdfs-over-ftp安装与配置
    mysql grant all privileges on
    Notepad++快捷键大全
    coconHashMap实现原理分析
  • 原文地址:https://www.cnblogs.com/karthuslorin/p/9297026.html
Copyright © 2020-2023  润新知