概述:
正则表达式是用于验证的一种表达式,它在js中是一个对象,被称为正则对象,对应的正则对象存在对应相关的元字符,我们只需要了解相关元字符及对应的可以书写一些简单的正则进行验证就可以了。
正则对象的声明
使用new关键词声明
//使用new关键字
//第一个参数填写相关正则表达式 添加修饰符(模式) g表示全局 i表示不区分大小写 m表示换行
var regx = new RegExp('abc','g')
console.log(regx)// /abc/g
使用//来修饰(常用的)
//第二种声明
var regx1 = /abc/g
console.log(regx1);// /abc/g
正则的匹配模式
-
g 全局匹配
-
i 不区分大小写
-
m 换行
-
s 单个匹配
正则的元字符
^开头
//^ 开头
var regx1 = /^a/ //表示以a开头
//字符串支持正则的四个方法 match 匹配 search 查找 split 分割 replace 替换
console.log('abc'.match(regx1));//['a']
$结尾
//$ 结尾
var regx2 = /b$/ //以b结尾
console.log('abc'.match(regx2));//null
[]表示其中一个元素
//[] 表示其中的任意一个元素
var regx3 = /^[abc]$/ //以abc其中一个开头及结尾 只能匹配a 或 b 或 c
console.log('abc'.match(regx3));//null
console.log('a'.match(regx3));//['a']
var regx3 = /^[abc][12]$/ //以abc中任意一个开头及以12中任意一个结尾 只能匹配a1 a2 b1 b2 c1 c2
console.log('a12'.match(regx3));//null
var regx3 = /^abc[abc]$/ //以abc开头,以abc其中任意一个结尾 abca abcb abcc
{}表示个数
-
{n}表示n个
-
{n,m}表示n个到m个
-
{n,}表示n个到无穷个 至少要有n个
//表示个数{}
//{1,2}1个到2个
var regx4 = /^[abc]{1,2}$/ //匹配abc中组成的1个或者两个组合
// 可以匹配 a b c aa ab ac bb bc ba ca cc cb
console.log('abc'.match(regx4));//null
console.log('a'.match(regx4));//['a']
console.log('ab'.match(regx4));//['ab']
//{1,} 一个及以上 至少要一个
var regx4 = /a{1,}/ //匹配一个a 或者是多个a
console.log('a'.match(regx4));//['a']
console.log('aaa'.match(regx4));//['aaa']
//{2} 表示两个
var regx4 = /^b{2}$/ //表示两个b
console.log('b'.match(regx4));//null
console.log('bb'.match(regx4));//['bb']
()分组
//()表示分组
var regx5 = /^([a][ac]){2}$/ //能匹配aaaa acac acaa
console.log('aaaa'.match(regx5));
console.log('acac'.match(regx5));
console.log('acaa'.match(regx5));
console.log('accc'.match(regx5));//null
+号表示一个到多个
//+表示{1,} 一个到多个
var regx6 = /^a+$/ //匹配一个a或者多个a
console.log('a'.match(regx6));//['a']
console.log('aaaa'.match(regx6));//['aaaa']
*号表示0个到多个
//*表示{0,} 0个到多个
var regx7 = /^a*b$/ //匹配0到多个a 以b结尾
console.log('b'.match(regx7));//['b']
console.log('ab'.match(regx7));//['ab']
console.log('aaaaab'.match(regx7));//['aaaaab']
?号表示0个到1个
//?表示0个到1个 {0,1}
var regx8 = /^a?b$/ //a可以有一个可以没有 以b结尾
console.log('b'.match(regx8));//['b']
console.log('ab'.match(regx8));//['ab']
console.log('aab'.match(regx8));//null
\d 表示数字 相当于[0-9] \D 表示非数字 相当于![0-9]
//数字表现形式
var regx9 = /^[0-9]$/ //匹配0-9之间的数字
console.log('1'.match(regx9));//['1']
console.log('0'.match(regx9));//['0']
//第二种数字表现形式 使用\d 表示为数字[0-9] \D 非数字
var regx9 = /^\d$/
console.log('1'.match(regx9));//['1']
console.log('0'.match(regx9));//['0']
var regx9 = /^\D$/
console.log('1'.match(regx9));//null
console.log(' '.match(regx9));//[' ']
\w 表示数字字母下划线 相当于[0-9A-Za-z] \W 表示非数字字母下划线![0-9A-Za-z]
//字母数字下划线 \w 表示数字字母下划线 \W 非数字字母下划线
var regx10 = /^[0-9A-Za-z_]$/ //表示数字字母下划线
console.log('1'.match(regx10));//['1']
console.log('A'.match(regx10));//['A']
console.log('a'.match(regx10));//['a']
console.log('_'.match(regx10));//['_']
console.log('?'.match(regx10));//null
var regx10 = /^\w$/
console.log('1'.match(regx10));//['1']
console.log('A'.match(regx10));//['A']
console.log('a'.match(regx10));//['a']
console.log('_'.match(regx10));//['_']
console.log('?'.match(regx10));//null
var regx10 = /^\W$/
console.log('?'.match(regx10));//['?']
console.log('a'.match(regx10));//null
. 表示所有的
//.表示所有的
var regx11 = /^.$/
console.log('?'.match(regx11));//['?']
console.log(' '.match(regx11));//[' ']
console.log('.'.match(regx11));//['.']
\s表示空白字符 \S表示非空白字符
//空白字符 \s空白字符 \S非空白字符 (空格 回车 制表符...)
var regx12 = /^\s$/
console.log('.'.match(regx12));//null
console.log(' '.match(regx12));//[' ']
var regx12 = /^\S$/
console.log('.'.match(regx12));//['.']
console.log(' '.match(regx12));//null
| 或者 表示其中的一种
var regx = /^([ab]{2})|([12]{3})$/ //表示的是ab其中的内容构成的两个 或者是123其中的内容构成的一个 任意其中一个
//匹配的内容 aa ab bb ba 或者是 111 121 211 122 112 222 212 221
console.log('121'.match(regx));//['121']
console.log('aa'.match(regx));//['aa']
//忽略掉后面的内容
console.log('aa1'.match(regx));//['aa']
转义(将本身的意思显示出来去除对应的元字符的功能)
-
将需要转移的内容放到[]里面
-
使用反斜杠‘\’来进行转义
//转义 需要匹配?或者.
//如果直接使用?和.他会解析为元字符 需要转义
//第一种直接把他加入中括号里面(在中括号里面会解析为字符串)
var regx = /^[?.]$/
console.log('?'.match(regx));
console.log('.'.match(regx));
//第二种方式 使用反斜杠\ 来进行转义 转义符合
var regx = /^\?|\.$/
console.log('?'.match(regx));
console.log('.'.match(regx));
关于正则对象的属性及方法
属性
-
lastIndex
console.log(regx.dotAll);//是否在正则表达式中一起使用"s"修饰符
console.log(regx.flags);//模式修饰
console.log(regx.gloabal);//是否全局匹配 g
console.log(regx.ingnoreCase);//是否区分大小写 i
console.log(regx.multiline);//是否换行 m
console.log(regx.unicode);//是否进行编码匹配
consoel.log(regx.source);//表示里面的内容
console.log(regx.sticky);//黏性
方法
-
test 测试是否符合当前的正则表达式(符合返回true 不符合返回false)
-
exec 执行方法(类似于match 返回一个数组 匹配不成功返回null)
var regx = /^[abc]{2}$/ //匹配ab aa ac bc bb ba ca cb cc
console.log(regx.lastIndex);//返回lastIndex表示最后的下标 他是可读可写的 默认值为0
//传入需要匹配的字符串 匹配成功返回true 失败返回false
console.log(regx.test('ab'));//true
console.log(regx.test('aaa'));//false
regx.lastIndex = 3;//自动默认更改 设置值为0
console.log(regx.test('ab'));//true
//执行方法 exec
console.log(regx.exec('ab'));//返回一个匹配的数组 类似match方法
console.log(regx.exec('abc'));//匹配不成功返回null
String 支持正则表达式的方法
-
search 搜索(根据对应的正则返回第一次找的下标 如果没有找到返回-1)
-
match 匹配(根据对应的正则表达式返回匹配的数组,如果没有匹配的返回null)
-
split 分割(根据对应的正则表达式进行字符串的分割 返回一个数组 如果不能进行分割返回的也是一个数组(数组里面只有一个元素))
-
replace 替换(根据对应的正则表达式进行字符串的替换返回一个新的字符串 如果不能完成替换返回原本的字符串)
本文围绕JavaScript中的正则表达式展开,介绍了正则对象的声明方式,包括用new关键词和//修饰;阐述了正则的匹配模式,如g全局匹配等;讲解了正则的元字符,像^开头、$结尾等;还说明了正则对象的属性及方法,以及String支持正则的相关方法。
9万+

被折叠的 条评论
为什么被折叠?



