正则语法总结

 

分组引用

在正则里面类似于这种的"\1"是对前面分组的引用.在replace这类方法中引用前面正则中的分组就要用"$1"这种来引用("\1和$1"都是对第一个分组引用,第二个就是"\2,$2")

‘2019-08-25‘.match(/(\d{4})-(\d{2})-\2/)
// null

‘2019-08-08‘.match(/(\d{4})-(\d{2})-\2/)
// 不为null
// 最后一个  ‘\2‘  是对第二个的引用 

// ES2018引用
‘2019-08-25‘.match(/(?<year>\d{4})-(?<month>\d{2})-\k<month>/)    // null

‘2019-08-08‘.match(/(?<year>\d{4})-(?<month>\d{2})-\k<month>/)    // 不为null
‘2019-08-25‘.replace(/(\d{4})-(\d{2})-(\d{2})/,`year($1),month($2)`)
// "year(2019),month(08)"
// $1,$2是对前两个匹配字符串的引用

相关文章:

  • 2022-12-23
  • 2021-09-15
  • 2022-01-15
  • 2021-05-02
  • 2021-05-01
  • 2021-09-14
  • 2021-11-19
猜你喜欢
  • 2021-10-01
  • 2021-08-23
  • 2021-08-15
  • 2021-10-18
  • 2021-10-12
相关资源
相似解决方案