如何使用正则表达式获取最后一个模式

How to get last pattern using regexpression

本文关键字:最后一个 模式 获取 正则表达式 何使用      更新时间:2023-09-26
var a = 'a.b.c.d.e.f'
a = a.split('.');
var len = a.length;
var pattern = a[len-2]+'.'+a[len-1]
console.log(pattern);

它工作绝对正常,但我必须使用正则表达式来做到这一点,有没有办法使用正则表达式获得相同的结果

或任何其他方法,这将是有效的解决方案,仅获取用 DOT (.) 字符分隔的最后 2 个字符串。

您可以使用正则表达式来查找以点分隔的最后一个字符。

/[^.]+'.[^.]+$/
  • [^.]+匹配以下列表中不存在的单个字符

    Quantifier: + 在一次到无限次之间,尽可能多地回馈,根据需要回馈[greedy]

    .文字字符.

  • '.与角色.字面上匹配

  • [^.]+匹配下面列表中不存在的单个字符

    Quantifier: + 在一次到无限次之间,尽可能多地回馈,根据需要回馈[greedy]

    .文字字符.

  • $字符串末尾断言位置

console.log('a.b.c.d.e.f'.match(/[^.]+'.[^.]+$/, ''));
console.log('zz.yy.xx.ww.vv.uu'.match(/[^.]+'.[^.]+$/, ''));
console.log('number with.dot'.match(/[^.]+'.[^.]+$/, ''));