美文网首页
正则表达式常用的方法

正则表达式常用的方法

作者: QinRenMin | 来源:发表于2018-09-05 19:05 被阅读0次

当初学习正则表达式没有系统的总结,当再看时知识体系不太完整,在后期学习中会带来一些麻烦,再总结,并且对于一些东西有了新的认知。

  • 创建方法
  1. 字面量创建
    let re = /正则表达式/标识(修饰符)/
    let re = /cm/g
    2.实例创建法
    let re = new RegExp(正则表达式,修饰符)
    let re = new RegExp('cm')
    两者的区别
    1实例创建法可以拼接字符
    let re = new RegExp('cm'+22)
    2字面量创建不需要转义
    let re = /\d/
    let re = new RegExp('\\d')
  • 常用方法
  1. est()
    含义:正则匹配字符串,返回布尔值
    写法:正则.test(字符串)
(function(){
    let str = 'abc';
    let re =/a/;
    let re1 =/abc/;
    let re2 =/w/;
    console.log(re.test(str)); //true
    console.log(re1.test(str)); //true
    console.log(re2.test(str)); //false
})();

2.search()
含义:正则匹配字符串,匹配成功返回匹配位置,否则返回-1,相当于indexOf
用法:字符串.search(正则)
注意:正则表达式的默认行为是区分大小写的
如果想要不区分大小写,在后面添加i
匹配成功则返回第一次匹配成功的值

(function () {
    let str = 'asd12zxc2as';
    let re = /S/;
    let re1 = /S/i;
    let re2 = /aS/i;
    console.log(str.search(re)); //-1
    console.log(str.search(re1)); //1
    console.log(str.search(re2)); //0
})();

3.match()
含义:正则匹配字符串,匹配成功返回匹配成功的数组,匹配不成功则返回null
用法:字符串.match(正则)

(function () {
    // let str = '123aa456qq1';
    let str = '1234561';
    let re = /\d/g;
    let re1 = /\d\d/g;
    let re2 = /\d\d\d/g;
    let re3 = /\d{1,5}/g;
    console.log(str.match(re)); //[ '1', '2', '3', '4', '5', '6' ]
    console.log(str.match(re1)); //[ '12', '34', '56' ]
    console.log(str.match(re2)); //[ '123', '456' ]
    console.log(str.match(re3)); //[ '12345', '61' ]
})();

4.exec()
lastIndex :这个属性记录的就是下一次捕获从哪个索引开始。
当未开始捕获时,这个值为0。
如果当前次捕获结果为null。那么lastIndex的值会被修改为0.下次从头开始捕获。
而且这个lastIndex属性还支持人为赋值。

let str = 'abc123cba456aaa789';
let reg = /\d+/g;  //此时加了标识符g
console.log(reg.lastIndex); //未捕获
// lastIndex : 0

console.log(reg.exec(str));
//  ["123", index: 3, input: "abc123cba456aaa789"];
console.log(reg.lastIndex);
// lastIndex : 6
//reg.lastIndex = 18; 修改lastIndex值,可以更改下一次匹配的位置
console.log(reg.exec(str)); 
//[ '456', index: 9, input: 'abc123cba456aaa789' ]
console.log(reg.lastIndex); //12

console.log(reg.exec(str));
 //[ '789', index: 15, input: 'abc123cba456aaa789' ]
console.log(reg.lastIndex); //18

console.log(reg.exec(str)); //null
console.log(reg.lastIndex); //0

console.log(reg.exec(str)); 
//[ '123', index: 3, input: 'abc123cba456aaa789' ]
console.log(reg.lastIndex); //6

5.replace()
含义:正则匹配字符串,用新的字符替换匹配成功的字符,匹配不成功返回原串
用法:字符串.replace(正则)

(function () {
    let str = 'asd12zxc2as';
    let re = /asd/;
    let re1 = /asd/;
    let re2 = /f/;
    console.log(str.replace(re,'q')); //q12zxc2as
    console.log(str.replace(re1,'w')); //w12zxc2as
    console.log(str.replace(re2,'s')); //asd12zxc2as
})();

replace第二个参数可以接收一个回调函数,注意其参数

(function(){
    let str = 'aasdEeAefhghhhce';
    let re = /e+/ig;
    str.replace(re,function(){
        console.log(arguments);
    })
    //{ '0': 'Ee', '1': 4, '2': 'aasdEeAefhghhhce' }
    // { '0': 'e', '1': 7, '2': 'aasdEeAefhghhhce' }
    // { '0': 'e', '1': 15, '2': 'aasdEeAefhghhhce' }
    //参数对应:匹配到的字符(串),下标,原串
    let str1 = 'aasdEeAefhghhhce';
    let re1 = /(e)\1*/ig;
    str1.replace(re1,function(){
        console.log(arguments);
    })
    // { '0': 'Ee', '1': 'Ee', '2': 4, '3': 'aasdEeAefhghhhce' }
    // { '0': 'e', '1': 'e', '2': 7, '3': 'aasdEeAefhghhhce' }
    // { '0': 'e', '1': 'e', '2': 15, '3': 'aasdEeAefhghhhce' }
    //参数对应:匹配到的字符(串),匹配到的首字符,下标,原串
})()

实例方法:test和exec
其他都是字符串方法

相关文章

  • 正则表达式汇总

    1.正则表达式一般命名为regex 2.JS正则表达式常用的方法 方法举例 3.常用限定符号 4.常用元字符 5....

  • 正则表达式

    什么是正则表达式?如何创建正则表达式正则表达式常用的方法字符串中的正则表达式常用的正则表达式假设用户需要在HTML...

  • 爬虫面试基础整理

    常用网络数据爬取方法urllib正则表达式Beautiful SoupSeleniumScrapyLxml 常见的...

  • 正则表达式与方法

    正则表达式---常用符号 正则表达式--常用函数 正则表达式--常用技巧 代码: 正则表达式的应用举例 1、使用f...

  • Python 正则表达式——re模块介绍

    Python 正则表达式 re 模块使 Python 语言拥有全部的正则表达式功能,re模块常用方法: re.ma...

  • Python 正则表达式——re模块介绍

    Python 正则表达式 re 模块使 Python 语言拥有全部的正则表达式功能,re模块常用方法: re.ma...

  • 总结js常用函数和常用技巧

    学习过程中总结的干货,包括常用函数、常用js技巧、常用正则表达式等。 Ajax封装 使用方法: 后台响应Ajax ...

  • 正则

    正则表达式全部符号解释(转) 常用正则表达式大全 (转) 不错的正则博客 正则的方法 var reg = /[1-...

  • JS 正则表达式

    语法 常用的方法: javascript中如何声明一个正则表达式对象在JavaScript当中,声明一个正则表达式...

  • Python中的正则表达式

    常用方法 通过 pandas 的 str.extract 方法 使用常规表达 正则表达式 一个神奇的网站,正则表达...

网友评论

      本文标题:正则表达式常用的方法

      本文链接:https://www.haomeiwen.com/subject/vsxzwftx.html