正则表达式对象

lastIndex 属性(正则表达式)

描述

指定索引,下一个匹配从该索引处开始。

语法

rgexp.lastIndex [= index]

lastIndex 属性的语法组成部分如下:

部分 描述
rgexp 必选项。正则表达式 对象。可以是变量名或文字。
index 索引,下一个匹配从该索引处开始。

说明

lastIndex 属性被 exec 方法、以及 String 对象的 match、replace、split 方法修改。

只有正则表达式使用了表示全局检索的 “g” 标志时,该属性才会起作用。此时应用下面的规则:

  • 如果 lastIndex 大于字符串的长度,则 regexp.testregexp.exec 将会匹配失败,然后 lastIndex 被设置为 0。

  • 如果 lastIndex 等于字符串的长度,且该正则表达式匹配空字符串,则该正则表达式匹配从 lastIndex 开始的字符串。(then the regular expression matches input starting at lastIndex.)
    如果 lastIndex 等于字符串的长度,且该正则表达式不匹配空字符串 ,则该正则表达式不匹配字符串,lastIndex 被设置为 0.。

  • 否则,lastIndex 被设置为紧随最近一次成功匹配的下一个位置。


source 属性

描述

返回正则表达式模式的文本的复本。只读。

语法

rgexp.source

rgexp 参数是正则对象。它可以是变量名或文字。

下面的例子举例说明了 source 属性的用法:

function SourceDemo(re, s)
{
    
    
  var s1;
  // 测试字符串中是否存在正则表达式。
  if (re.test(s))
    s1 = " contains ";
  else
    s1 = " does not contain ";
  // 获得正则表达式自己的文本。
  return(s + s1 + re.source);
}

常用匹配

获取两个字符之间的内容

  1. 获取某个字符之前的字符串: str.match(/(\S*)a/)[1]
  2. 获取某个字符之后的字符串:str.match(/a(\S*)/)[1]
  3. 获取两个字符串之间的字符串:str.match(/a(\S*)b/)[1]

注意事项

小心 g

全局匹配符有个陷阱,多次匹配同一个字符串,可能会出现不同的结果

引用 mdn 文档说明:

​ 如果正则表达式设置了全局标志,test()的执行会改变正则表达式 lastIndex属性。连续的执行test()方法,后续的执行将会从 lastIndex 处开始匹配字符串,(exec() 同样改变正则本身的 lastIndex属性值).

const reg = /hi/g
  console.log(reg.test('hi')) true
  console.log(reg.test('hi')) false
  console.log(reg.test('hi')) true

三次检验的结果,不一样

这里说明下 lastIndex ,这个是 RegExp 对象的属性,只当使用了 g 的时候,才会有用

mdn

lastIndex 是正则表达式的一个可读可写的整型属性,用来指定下一次匹配的起始索引。

具体规则:

  • 如果 lastIndex 大于字符串的长度,则 regexp.testregexp.exec 将会匹配失败,然后 lastIndex 被设置为 0。
  • 如果 lastIndex 等于字符串的长度,且该正则表达式匹配空字符串,则该正则表达式匹配从 lastIndex 开始的字符串。(then the regular expression matches input starting at lastIndex.)
  • 如果 lastIndex 等于字符串的长度,且该正则表达式不匹配空字符串 ,则该正则表达式不匹配字符串,lastIndex 被设置为 0.。
  • 否则,lastIndex 被设置为紧随最近一次成功匹配的下一个位置。

下面解释下每种情况的逻辑:

const reg = /hi/g
console.log(reg.lastIndex, reg.test('hi')) // 0 true
console.log(reg.lastIndex, reg.test('hi')) // 2 false;触发了第三条规则,匹配失败,然后 lastIndex 设置为 0;因为 test 方法是从 lastIndex 开始匹配,所以从第三位字符串匹配的时候,失败了
console.log(reg.lastIndex, reg.test('hi')) // 0 true
console.log(reg.lastIndex) // 2
const reg = /\w*/g
console.log(reg.lastIndex, reg.test('hi')) // true
console.log(reg.lastIndex, reg.test('1hi')) // 2 true
console.log(reg.lastIndex, reg.test('hi')) // 3 false;触发第一条规则,lastIndex 大于字符串长度,所以变位0,且失败
console.log(reg.lastIndex) // 0

使用 g 时,尽量不要缓存该正则

猜你喜欢

转载自blog.csdn.net/WuLex/article/details/109026302
今日推荐