简体   繁体   English

JavaScript REGEX负面展望

[英]JavaScript REGEX negative look ahead confusion

OK I processing some lines with JavaScript and wanted to use a negative look ahead to eliminate the line if it contained a particular phrase but I still seem to be grabbing the lines and regexbuddy and others show it matching and I am not sure why 好的,我使用JavaScript处理了一些行,并希望使用否定的前瞻来消除该行(如果其中包含特定短语),但是我仍然似乎抓住了这些行,而regexbuddy却发现其他行与之匹配,我不确定为什么

^This (\w+) (\w+) was built in (\d+). (.+?).(?! This car has a salvage title.)$

So I need to grab some various second sentence values that are to varied to match thus the (.+?). 因此,我需要获取一些不同的第二句值,以使其与(。+?)相匹配。 what I don't want is if the line contains has the reference to the salvage title 我不想要的是如果该行包含对打捞标题的引用

SO 所以

This Honda Accord was built in 1986. This car has 3 accidents. This car has a salvage title.

should not match because the negative look ahead should fail 不匹配,因为负面的展望应该失败

but

This Honda Accord was built in 1986. This car has 3 accidents.

should match but I am getting matches on both. 应该匹配,但我都匹配。 Am I just mis-understanding the negative look ahead or am I mis-formatting it? 我是只是误解了未来的消极面貌还是在错误格式化?

 …(?! This car has a salvage title.)$ 

You've put the negative lookahead right before the end of the string. 您已将否定的lookahead放在字符串末尾之前。 It will always not match there (ie succeed). 永远不会在那里匹配(即成功)。 It's a lookahead after all, not a lookbehind, and peeking behind the end of the string doesn't make sense. 毕竟这是一个先行,而不是向后看,在字符串末尾窥视是没有意义的。

You probably wanted to do this instead: 您可能想这样做:

^This (\w+) (\w+) was built in (\d+)\. (?!This car has a salvage title)(.+?)\.$

Here, the (?!This car has a salvage title)(.+?) does match the sentence (precisely, at least one character) before the dot, which does not begin with the phrase "This car has a salvage title". 在此, (?!This car has a salvage title)(.+?)与点之前的句子(精确地,至少一个字符)匹配,该句子不以短语“这辆车具有打捞标题”开头。 Or, if you don't want that phrase anywhere after the first sentence, then use 或者,如果您不想在第一句话后的任何地方使用该短语,请使用

^This (\w+) (\w+) was built in (\d+)\. (?!.*?This car has a salvage title)(.+?)\.$
//                                        ^^^

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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