js中的正则介绍

正则基础

  • 正则用于处理字符串
  • 正则对象.test(字符串);这是正则匹配。用于检查字符串是否符合正则,符合返回true,不符合返回false
  • 正则对象.exec(字符串)这是正则捕获。
  • 返回结果是数组,如果捕获不到返回null
  • 第一项:代表捕获到的内容
  • 第二项:index表示捕获到的内容在原字符串中的索引
  • 第三项:input表示要捕获的整个字符串
  • 第四项:groups表示分组
  • 正则捕获具有懒惰性,解决方案是在正则表达式后面加上修饰符g。正则的实例都具有一个lastIndex属性,用于记录开始正则捕获时开始的字符串的索引
var reg = /\d+/;
console.log(reg.exec("zhufeng12peixun34"));//["12",index:7,input:"zhufeng12peixun34",groups:undefined]
console.log(reg.exec("zhufeng12peixun34"));//["12",index:7,input:"zhufeng12peixun34",groups:undefined]
//上面这个例子进行两次正则捕获都不能捕获到字符串中的34这组数字
var reg = /\d+/g;
console.log(reg.lastIndex);//0
console.log(reg.exec("zhufeng12peixun34"));//["12",index:7,input:"zhufeng12peixun34",groups:undefined]
console.log(reg.lastIndex);//9
console.log(reg.exec("zhufeng12peixun34"));//["34",index:9,input:"zhufeng12peixun34",groups:undefined]
console.log(reg.lastIndex);//9
console.log(reg.exec("zhufeng12peixun34"));//["34",index:15,input:"zhufeng12peixun34",groups:undefined]
console.log(reg.lastIndex);//17
console.log(reg.exec("zhufeng12peixun34"));//null
console.log(reg.lastIndex);//0。在捕获到的结果是null时,reg.lastIndex会重新设置为0,开始重新捕获
console.log(reg.exec("zhufeng12peixun34"));//["12",index:7,input:"zhufeng12peixun34",groups:undefined]
  • 正则捕获具有贪婪性,通过将?放在量词的后面取消捕获的贪婪性
var reg = /^\d+?$/;//表示存在一个或多个数字
console.log(reg.test("1"));//true
console.log(reg.test("011"));//true
var reg = /\d+?/g;
console.log(reg.exec("zhufeng20peixun19"));//["2", index: 7, input: "zhufeng20peixun19", groups: undefined]
console.log(reg.exec("zhufeng20peixun19"));//["0", index: 8, input: "zhufeng20peixun19", groups: undefined]
console.log(reg.exec("zhufeng20peixun19"));//["1", index: 15, input: "zhufeng20peixun19", groups: undefined]
console.log(reg.exec("zhufeng20peixun19"));//["9", index: 16, input: "zhufeng20peixun19", groups: undefined]
var reg = /\d*?/;
console.log(reg.test("abc"));//true
console.log(reg.test("123"));//true
console.log(reg.test("abc123"));//true
console.log(reg.lastIndex);//0
console.log(reg.exec("abc"));//["", index: 0, input: "abc", groups: undefined]
console.log(reg.lastIndex);//0
var reg = /\d*?/g;//应用场景不多,没有什么意义
console.log(reg.lastIndex);//0
console.log(reg.exec("a123b"));//["",index:0,input:"a123b",groups:undefined]
console.log(reg.lastIndex);//0
console.log(reg.exec("a123b"));//["",index:0,input:"a123b",groups:undefined]
var reg = /\d{2}?/g;//与 /\d{2,}?/g 正则捕获的结果一样,都是取最少的次数进行捕获
console.log(reg.lastIndex);//0
console.log(reg.exec("a123bc"));//["12",index:1,input:"a123bc",groups:undefined]
console.log(reg.lastIndex);//3
console.log(reg.exec("a123bc"));//null
  • 分组中的正则捕获,先进行整个正则表达式的捕获,然后从左向右依次进行每个小正则的捕获

注意: test exec两个方法都在类RegExp的原型上,凡是RegExp的实例都能调用者两个方法

  • 正则包括元字符和修饰符
  • 修饰符
  • i:ignoreCase忽略大小写
  • g:global全局捕获
  • m:multiline多行匹配
  • 元字符
  • 量词元字符
  • {m}:代表前面的字符出现m次
  • {m,}:代表前面的字符出现m次到多次
  • {m,n}:代表前面的字符出现m到n次
  • *:代表前面的字符出现0次或多次
  • +:代表前面的字符出现1次或多次
  • {?}:代表前面的字符出现0次或1次
  • []
  • [a-z]:表示a-z中的任意一个字符
  • [abc]:表示abc中的任意一个字符
  • [0-9]:表示0-9中的任意一个数字
  • [x|y]:表示x或y或|中的任意一个字符
  • [^abc]:表示除了abc以外的任意一个字符

注意: []中不能识别两位或两位以上的数字

  • 其他元字符
  • \:转义字符,它可将普通字符转换为有特殊含义字符,或将有特殊含义的字符转化为普通字符
  • ^:表示以什么开头
  • $:表示以什么结尾
  • .:表示除了\n \r以外的任意字符
  • \n:表示换行符
  • \b:表示单词的边缘
  • \d:匹配0-9中的任意一个数字
  • \D:匹配除了0-9以外的任意一个数字
  • \w:匹配字母、数字、下划线中的任意一个字符
  • \W:匹配除了字母、数字、下划线中的任意一个字符
var reg = /1-9/;
console.log(reg.test("1-9"));//true
var reg = /\./;//表示一个普通的点。此处使用转义字符将.转化为普通的点
console.log(reg.test("1.2"));//true
var reg = /\d/;//表示字符串包含一个0-9之间的任意一个数字
console.log(reg.test("123"));//true
console.log(reg.test([1,2,3]));//true。在正则匹配时参数不是字符串数据类型会先转化为字符串类型再匹配。[1,2,3]转成字符串是"1,2,3"
console.log(reg.test({}));//false。在正则匹配时参数不是字符串数据类型会先转化为字符串类型再匹配。{}转成字符串是"[object Object]"
var reg = /e/;
console.log(reg.test({}));//true
var reg = /^\d$/;//^代表以什么开头,$代表以什么结尾。代表字符串只能有一个数字
console.log(reg.test("123"));//false
console.log(reg.test("1"));//true
var reg = /^2\d{2}/;//表示以2开头,后面出现两个数字均可
console.log(reg.test("2111"));//true
console.log(reg.test("21a1"));//false
console.log(reg.test("21"));//false
var reg = /^2\d{2}$/;//表示以2开头,并且后面只有两位数字
console.log(reg.test("21"));//false
console.log(reg.test("21a"));//false
console.log(reg.test("214"));//true
console.log(reg.test("2112"));//false
var reg = /d/;//代表字符串含有d即可
console.log(reg.test("ad"));//true
var reg = /^[13-59]$/;//表示1,3-5,9中的任意一个数字
console.log(reg.test("18"));//false
console.log(reg.test("4"));//true
//------------------------//
var reg = /\d/;
console.log(reg.exec("px1234px"));//['1',index:2,input:'px1234px',groups:undefined]
var reg = /\d{4}/;
console.log(reg.exec("px123px"));//null
var reg = /2/;
console.log(reg.exec([1,3,2,2]));//['2',index:4,input:'1,3,2,2',groups:undefined]
var reg = /O/;
console.log(reg.exec({}));//['O',index:8,input:'[object Object]',groups:undefined]
var reg = /^2/;
console.log(reg.exec("32"));//null
var reg = /\d$/;
console.log(reg.exec("32"));//['2',index:1,input:'32',groups:undefined]
//-----------------------//
var reg = /ABC/;
console.log(reg.test("ABc12"));//false
var reg = /ABC/i;//使用了修饰符i
console.log(reg.test("ABc12"));//true

正则举例

// 手机号:/^1\d{10}$/
// 有效数字:/^[+-]?(\d|([1-9]\d+))(\.\d+)?$/
// 身份证号:130601 1999 01 20 101 X
var reg = /^(\d{6})([12]\d{3})((0[1-9])|(1[0-2]))((0[1-9])|([12]\d)|(3[01]))(\d{3})([0-9X])$/;
// 年龄18-65: /^(1[89])|([2-5][0-9])|(6[0-5])$/
// 邮箱正则的一种:/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/

正则创建方式

  • 字面量方式创建

局限性:当正则中有变量时这种方式不能使用

var name = "zhufeng";
var reg = /\dname/;
console.log(reg.test("1zhufeng"));//false
console.log(reg.test("21namer"));//true
  • 实例方式创建

解决正则中存在变量的问题

var name = "zhufeng";
var reg = new RegExp("\\d"+name);//注意:因为字符串中\是转义字符,如果想让它变成普通字符,需要前面再加上\将其转义成普通字符
console.log(reg.test("1zhufeng"));//true
var reg1 = new RegExp(reg);//可以利用已知的正则对象创建新的正则对象

正则分组

使用小括号进行正则分组。小括号的作用:

  • 改变正则优先级
var reg = /^z1[a-z]|food$/;//表示以z1[a-z]开头或以food结尾。注意:|左右两边是一个整体
console.log(reg.test("z1"));//false
console.log(reg.test("z1afoo"));//true
console.log(reg.test("food"));//true
var reg = /^123(z1[a-z])|food[1-9]$/;//表示以123(z1[a-z])开头或以food[1-9]结尾
console.log(reg.test("123z1a"));//true
console.log(reg.test("123bbbb"));//false
console.log(reg.test("123z1a"));//true
console.log(reg.test("ddfood9"));//true
var reg = /^(z|food)$/;//表示字符串是'z'或'food'
console.log(reg.test("zood"));//false
console.log(reg.test("1food"));//false
console.log(reg.test("food"));//true
console.log(reg.test("z"));//true
  • 括号引用:\1 \2代表和第一个分组或第二个分组出现一模一样的内容
var reg = /^(a)(n)\2\1$/;
console.log(reg.test("an"));//false
console.log(reg.test("anna"));//true
console.log(reg.test("annna"));//false
  • 分组捕获:从左到右依次捕获
var reg = /^[+-]?(\d|([1-9]\d+))(\.\d+)?$/;//匹配一个数字
console.log(reg.exec("12.11"));//["12.11","12","12",".11",index:0,input:"12.11",groups:undefined]
var reg = /^[+-]?(\d|[1-9]\d+)(\.\d+)?$/;//匹配一个数字,这个写法和上面这个写法的区别在于正则捕获时上面捕获到的内容多一项,因为正则捕获时小括号中的都需要捕获
console.log(reg.exec("12.11"));//["12.11","12",".11",index:0,input:"12.11",groups:undefined]
var reg = /^\d{6}([12]\d{3})((0[1-9])|(1[12]))((0[1-9])|([1-2][0-9])|(3[01]))(\d{3})([0-9X])$/;//表示身份证号
console.log(reg.exec("13062219900824102X"));//["13062219900824102X","1990","08","08",undefined,"24",undefined,"24",undefined,"102","X",index:0,input:"13062219900824102X",groups:undefined]。它隐藏的属性length:11
var reg = /^\d{6}([12]\d{3})(0[1-9]|1[12])((0[1-9])|([1-2][0-9])|(3[01]))(\d{3})([0-9X])$/;//表示身份证号
//注意:在有两个或两个以上的|连接时就不能理解为左边一部分右边一部分,他的组合方式有很多,此时需要用括号分组,在有一个|连接时可以理解为左边一部分右边一部分

正则正/负向预查

注意: 正向预查和负向预查不能同时跟着^ $一起使用,但可以跟着^$使用

  • 正向预查?=:小括号中的内容只匹配,不捕获
var reg = /zhufeng(?=peixun)/;//表示zhufeng后面必须跟着peixun,在正则捕获时不捕获peixun
console.log(reg.test("123zhufengpeixun456"));//true
console.log(reg.test("123zhufeng456"));//false
console.log(reg.exec("123zhufengpeixun456"));//["zhufeng",index:3,input:"123zhufengpeixun456",groups:undefined]
console.log(reg.exec("123zhufeng456"));//null
  • 负向预查?!:小括号中的内容只匹配,不捕获
var reg = /zhufeng(?!peixun)/;//表示含有zhufeng并且后面不能跟着peixun
console.log(reg.test("123zhufengpeixun456"));//false
console.log(reg.test("123zhufeng456"));//true
console.log(reg.test("123zhufengpei456"));//true
console.log(reg.exec("123zhufengpeixun456"));//null
console.log(reg.exec("123zhufengpei"));//["zhufeng",index:3,input:"123zhufengpei",groups:undefined]

replace方法

字符串的replace方法中第一个参数是正则时,第二个参数可以是字符串,也可以是回调函数

var str = "zhufengzhufeng";
var newStr = str.replace(/zhufeng/g,function(){
	//1、正则捕获几次回调函数就执行几次
	//2、用每次将回调函数的返回结果替换正则匹配的内容
	//3、回调函数中的arguments表示每次正则捕获的结果,是一个类数组[捕获到的字符串,捕获到的字符串在元字符串中的索引,原字符串,...]
	console.log(arguments);
	// ["zhufeng", 0, "zhufengzhufeng", callee: ƒ, Symbol(Symbol.iterator): ƒ]
    // ["zhufeng", 7, "zhufengzhufeng", callee: ƒ, Symbol(Symbol.iterator): ƒ]
	return "zhufengpeixun";
});
console.log(newStr);//"zhufengpeixunzhufengpeixun"
var newStr = str.replace(/zhufeng/,function(){//此处正则中没有使用修改符g所以只捕获一次,回调函数也只执行一次
    console.log(arguments);
    // ["zhufeng", 0, "zhufengzhufeng", callee: ƒ, Symbol(Symbol.iterator): ƒ]
    return 'zhufengpeixun';
});
console.log(newStr);//'zhufengpeixunzhufeng'

replace应用

//1、将下面字符串中所有单词首字母大写
var str = "my  name  is  666,i am 22 years old,i like study";
var newStr = str.replace(/\b[a-z]/g,function(){
	return arguments[0].toUpperCase();
});
console.log(newStr);//"My  Name  Is  666,I Am 22 Years Old,I Like Study"

//2、将小写数字转换为大写数字
var str = '20190722';
var ary = ["零","壹","贰","叁","肆","伍","陆","柒","捌","玖"];
var newStr = str.replace(/\d/g,function(){
	return ary[arguments[0]];
});
console.log(newStr);//"贰零壹玖零柒贰贰"

//3、将下面的日期格式进行转换
var str = "2019-07-22 16:40:30";
var ary = ["年","月","日","时","分","秒"];
//方案一
var i=0;
var newStr = str.replace(/\d{2}\b\W?/g,function(){
	return arguments[0].substr(0,2)+ary[i++];
});
console.log(newStr);//"2019年07月22日16时40分30秒"
//方案二
var i = 0;
var s = "";
var newStr = str.replace(/\d+/g, function () {
    s += arguments[0] + ary[i++]
})
console.log(s);

match方法

  • 它是字符串原型上的方法
  • 利用该方法可以通过正则捕获,获取到一个数组
var reg = /\d+/g;
var str = "zhufeng19peixun09";
console.log(str.match(reg));//["19","09"]
发布了19 篇原创文章 · 获赞 3 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/u010510187/article/details/96630932