您好,欢迎来到二三四教育网。
搜索
您的当前位置:首页第五章(3):正则表达式

第五章(3):正则表达式

来源:二三四教育网

正则表达式

正则表达式,又称正规表示式、正规表示法、正规表达式、规则表达式、常规表示法(英语:Regular Expression,在代码中常简写为regex、regexp或RE),是计算机科学的一个概念。正则表达式使用单个字符串来描述、匹配一系列匹配某个句法规则的字符串。在很多文本编辑器里,正则表达式通常被用来检索、替换那些匹配某个模式的文本。

ECMAScript通过RegExp类型来支持正则表达式。

创建正则表达式

创建正则表达式有两种方式。通过构造函数和字面量的方式。如下:

var reg = /hello/ // 字面量
var regexp = new RegExp("hello")  // 构造函数
元字符

元字符包括: ( [ { \ ^ $ * + ? . | } ] )。这些字符在正则中有特殊的用途。如果要匹配的字符串中包括这些符号的话,必须通过 \ 来转义。例如:

var pattern = /[a-z]/
var pattern1 = /\[a-z\]/
console.log(pattern.exec('adf'))    // ["a", index: 0, input: "adf"]
console.log(pattern1.exec('adf'))   // null

console.log(pattern.exec('[a-z]'))  // ["[a-z]", index: 0, input: "[a-z]"]
console.log(pattern1.exec('adf')) // null
正则表达式的标识
  1. g 全局模式
var regG = /m/g
console.log(('my name is Test').match(regG))  // ["m", "m"]
console.log(regG.exec('my name is Test')) // ["m", index: 0, input: "my name is Test"]
  1. i 不区分大小写
// i 不区分大小写
var regI = /h/i
console.log(regI.test('He'))  // true
正则表达式的方法
  1. test
// test 测试字符串是否匹配正则表达式,返回true或者false
var reg = /^(\d{3}-\d{4}-\d{4})$/
console.log(reg.test('187-0127-9074'))  // true
console.log(reg.test('187-0127-90745asd'))  // true
  1. replace
// replace 字符串中执行查找匹配的String方法,并且使用替换字符串替换掉匹配到的子字符串。
var strRe = ("187-0127-9074").replace(reg, '电话号码')
console.log(strRe)  // 电话号码
  1. exec和match

exec和match有些类似。exec是正则表达式的方法,match是字符串的方法。exec和match都会返回数组。但是在使用g标志的时候,match会返回一个数组,包括所有匹配的字符串。而exec只会返回第一个匹配的字符。

exec返回包含一个匹配信息的数组,如果没有匹配则返回null。数组是Array的实例。包含额外两个属性:index和input。index表示匹配的字符串的索引值,input表示应用正则的字符串。数组中的第一项是匹配的字符串,第二项至第N项是捕获括号中对应的字符串的值。如果没有捕获括号,则只有第一项。match在不带g标识的时候表现和exec类似。

exec

// exec 在字符串中查找是否匹配,返回一个数组。找不到则返回null.
var reg = /^(\d{3}-\d{4}-\d{4})$/
console.log(reg.exec('187-0127-9074187-0127-9074')) // null
console.log(reg.exec('187-0127-9074'))  // ["187-0127-9074", "187-0127-9074", index: 0, input: "187-0127-9074"]

match

// match 在字符串中查找是否匹配,返回一个数组。找不到则返回null。
console.log(('187-0127-9074187-0127-9074').match(reg)) // null
console.log(('187-0127-9074').match(reg))  // ["187-0127-9074", "187-0127-9074", index: 0, input: "187-0127-9074"]

exec和match的区别

// g 全局模式
var regG = /m/g
console.log(('my name is Test').match(regG))  // ["m", "m"]
console.log(regG.exec('my name is Test')) // ["m", index: 0, input: "my name is Test"]
正则表达式的特殊变量
//使用特殊字符
  /**
   * ^ 匹配输入的开始
   * \ 将特殊字符转义为普通字符
   * @type {RegExp}
   */
  var reg1 = /^123/g
  console.log(reg1.exec('123456'))  // ["123", index: 0, input: "123456"]
  var reg1_1 = /\^123/g
  console.log(reg1_1.exec('123456'))  // null
  console.log(reg1_1.exec('^123456 ^1234 ^1235'))  // ["^123", index: 0, input: "^123456"]

  // $ 匹配输入的结束
  var reg2 = /fan$/
  console.log(reg2.exec('test fan'))  // ["fan", index: 5, input: "test fan"]
  console.log(reg2.exec('test fanc'))  // null

  // * 匹配前一个表达式0次或者多次,等价于{0,}
  var reg3 = /re*/
  console.log(reg3.exec('great')) // ["re", index: 1, input: "great"]
  console.log(reg3.exec('grceat'))  // ["r", index: 1, input: "grceat"]

  // + 匹配前一个表达式1次或者多次,等价于 {1,}
  var reg4 = /l+/
  console.log(reg4.exec('I like lol')) // ["l", index: 2, input: "I like lol"]

  // ? 匹配前一个表达式0次或者1次。等价于 {0,1}
  var reg5 = /a?b/
  console.log(reg5.exec('abcd'))  // ["a", index: 0, input: "abcd"]
  console.log(reg5.exec('bcd')) // ["b", index: 0, input: "bcd"]

  // . 匹配除换行符之后的任何单个字符
  var reg6 = /.e/
  console.log(reg6.exec('e')) //  null
  console.log(reg6.exec('love'))  // ["ve", index: 2, input: "love"]

  // (x) 匹配x并记住匹配项,括号被称为 捕获括号
  var reg7 = /(a)(b)+/
  console.log(reg7.exec('a')) // null
  console.log(reg7.exec('abc')) // ["ab", "a", "b", index: 0, input: "abc"]

  // (?:x) 匹配 'x' 但是不记住匹配项。
  var reg8 = /(?:name){1,2}/
  console.log(reg8.exec('name123'))   // ["name", index: 0, input: "name123"]

  // x(?=y) 匹配'x'仅仅当'x'后面跟着'y'.这种叫做正向肯定查找。
  var reg9 = /name(?=tiptoe)/
  console.log(reg9.exec('nametiptoe123')) // ["name", index: 0, input: "nametiptoe123"]
  console.log(reg9.exec('namet123'))  // null

  // x(?!y) 匹配'x'仅仅当'x'后面不跟着'y',这个叫做正向否定查找。
  var reg10 = /name(?!tiptoe)/
  console.log(reg10.exec('nametiptoe')) // null
  console.log(reg10.exec('namet123')) // ["name", index: 0, input: "namet123"]

  // x|y 匹配x或者y
  var reg11 = /yes|no/
  console.log(reg11.exec('yeno')) // ["no", index: 2, input: "yeno"]
  console.log(reg11.exec('yesn')) // ["yes", index: 0, input: "yesn"]

  // {n} 匹配了前面一个字符刚好发生了n次。
  var reg12 = /1{2}/
  console.log(reg12.exec(1))  // null
  console.log(reg12.exec(11)) // ["11", index: 0, input: "11"]

  // {n,m} 匹配前面的字符至少n次,最多m次。
  var reg13 = /1{1,5}/
  console.log(reg13.exec(123456)) // ["1", index: 0, input: "123456"]
  console.log(reg13.exec(111111111123456))  // ["11111", index: 0, input: "111111111123456"]

  // [xyz] 一个字符集合。匹配方括号的中任意字符,包括转义序列。 你可以使用破折号(-)来指定一个字符范围。
  var reg14 = /[a-z]/
  console.log(reg14.exec('abc123def'))  // ["a", index: 0, input: "abc123def"]
  console.log(reg14.exec('123'))  // null

  // [^xyz] 反向字符集合
  var reg14 = /[^a-z]/
  console.log(reg14.exec('abc123def'))  // ["1", index: 3, input: "abc123def"]
  console.log(reg14.exec('123'))  // ["1", index: 0, input: "123"]

  // /d 匹配一个数字 等价于[0-9]
  var reg15 = /\d/
  console.log(reg15.exec('0123zzz'))  // ["0", index: 0, input: "0123zzz"]
  console.log(reg15.exec('zzz'))  // null

  // /D 匹配一个非数字 等价于[0-9]
  var reg16 = /\D/
  console.log(reg16.exec('0123zzz'))  // ["z", index: 4, input: "0123zzz"]
  console.log(reg16.exec('zzz'))  // ["z", index: 0, input: "zzz"]

  // \w 匹配一个单字字符(字母、数字或者下划线)。 等价于[A-Za-z0-9_]。
  var reg17 = /\w/
  console.log(reg17.exec('@#')) // null
  console.log(reg17.exec('_123')) // ["_", index: 0, input: "_123"]

  // \W 匹配一个非单字字符。 等价于[^A-Za-z0-9_]。
  var reg18 = /\W/
  console.log(reg18.exec('@#')) // ["@", index: 0, input: "@#"]
  console.log(reg18.exec('_123')) // null

引用

  • javascript高级程序设计
  • [彻底领悟javascript中的exec与match方法]()

Copyright © 2019- how234.cn 版权所有 赣ICP备2023008801号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务