正则表达式:检查`abc`然后检查`def`而不用`123`

前端之家收集整理的这篇文章主要介绍了正则表达式:检查`abc`然后检查`def`而不用`123`前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何使用 Regex查找以abc开头的每个字符串,以def结尾,但两者之间不包含123?

解决方法

你可以在这里使用否定前瞻.

^abc(?:(?!123).)*def$

正则表达式

^              # the beginning of the string
abc            # 'abc'
(?:            # group,but do not capture (0 or more times)
 (?!           # look ahead to see if there is not:
  123          # '123'
 )             # end of look-ahead
 .             # any character except \n
)*             # end of grouping
 def           # 'def'
$             # before an optional \n,and the end of the string

猜你在找的正则表达式相关文章