正则表达式

概述:

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

正则对象的声明

1.使用new关键词声明

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

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

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

支持正则的四个方法

  • search 查找(根据对应的正则返回第一次找的下标 如果没有找到返回-1)
  • match 匹配  (根据对应的正则表达式返回匹配的数组,如果没有匹配的返回null)
  • split 切割(根据对应正则表达式进行字符串的分割 返回一个数组 如果不能进行分割返回的也是一个数组 (数组里面只有一个元素))
  • replace 替换(根据对应的正则表达式进行字符串的替换返回一个新的字符串 如果不能完成替换返回原本的字符串)

正则的匹配模式

  • 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
+ 表示一个到多个
//+表示 {1,} 一个到多个
var regx6 = /^a+$/ //匹配一个a或者多个a
console.log('a'.match(regx6));//['a']
console.log('aaaa'.match(regx6));['aaaa']
\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
\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']

关于正则对象的属性及方法

属性
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

     

lastIndex

 

原文地址:http://www.cnblogs.com/tch001/p/16833481.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性