简体   繁体   中英

javascript regexp negative lookbehind doesn't match in some case

i use node 10.15

i want the expression to match only line2 but not line 1 and line 3

在此处输入图片说明

why this negative lookbehind failed?

(?<!月)\d+日(?!后)

在负数后面添加一个数字:

(?<!月|\d)\d+日(?!后)

It's fairly easy. You only have to guarantee, that the match is not preceded by a digit.

(?<!月|\d)\d+日(?!后)
------^^^

This adds contrast to your RegEx. See RexEgg for details

See here for online example

As you are using Negative look behind, you should be on EcmaScript2018.

You should put a word boundary \\b before \\d in your regex to avoid it matching partially and then it will stop matching your first line. Try this regex,

(?<!月)\b\d+日(?!后)

Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM