否定前瞻正则表达式 [英] Negative lookahead Regular Expression
本文介绍了否定前瞻正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望匹配以.htm结尾的所有字符串,除非它以foo.htm结尾。我对正则表达式一般都很体面,但负面的前瞻让我难过。为什么这不起作用?
I want to match all strings ending in ".htm" unless it ends in "foo.htm". I'm generally decent with regular expressions, but negative lookaheads have me stumped. Why doesn't this work?
/(?!foo)\.htm$/i.test("/foo.htm"); // returns true. I want false.
我应该使用什么?我想我需要一个负面看背后表达式(如果JavaScript支持这样的事情,我知道它没有)。
What should I be using instead? I think I need a "negative lookbehind" expression (if JavaScript supported such a thing, which I know it doesn't).
推荐答案
问题非常简单。这样做:
The problem is pretty simple really. This will do it:
/ ^(?!。* foo \.htm $)。* \。htm $ / i
这篇关于否定前瞻正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文