JavaScript中的正则表达式

概述:

正则表达式是运用于验证一种表达式,他在js中是一个对象,被称为正则对象,对应的正则对象存在对应相关的元字符。我只需要了解相关元字符及对应的可以书写一些简单的正则进行验证就可以了。

正则对象的声明

使用new关键词声明

//使用new关键字 g表示全局  
//第一个参数填写相关正则表达式  添加修饰符(匹配模式) g 表示 全局 i 表示 不区分大小写 m 表示换行
var regx = new RegExp('abc','g')
console.log(regx);

使用//来修饰 (常用的)

// 第二种声明
var regx1 = /abc/g
console.log(regx1);

正则的匹配模式

  • 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,} 一个及以上 至少要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或者多个a 以b结尾
console.log('b'.match(regx7));//['b']
console.log('ab'.match(regx7));//['a']
console.log('aaaab'.match(regx7));['aaaa']

? 表示0个到一个

//?表示0个到一个 {0,1}
var regx8 = /^a?b$/ //a可以有1个可以没有 以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.global);//是否全局匹配 g
    console.log(regx.ignoreCase);//是否区分大小写 i
    console.log(regx.multiline);//是否换行 m
    console.log(regx.unicode);//是否进行编码匹配
    console.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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值