简体   繁体   English

如何在JavaScript中反转正则表达式?

[英]How can I invert a regular expression in JavaScript?

I have a string A and want to test if another string B is not part of it. 我有一个字符串A,想测试另一个字符串B是否不属于其中。 This is a very simple regex whose result can be inverted afterwards. 这是一个非常简单的正则表达式,其结果随后可以反转。

I could do: 我可以做:

/foobar/.test('foobar@bar.de')

and invert it afterwards, like this: 然后将其反转,如下所示:

!(/foobar/).test('foobar@bar.de') 

The problem I have is, that I need to do it within the regular expression and not with their result. 我的问题是,我需要在正则表达式中执行此操作,而不要使用它们的结果。 Something like: 就像是:

/!foobar/.test('foobar@bar.de')

(which does not work) (不起作用)

In other words: the regular expression should test for a non-existence and return true in that case. 换句话说:正则表达式应测试是否存在,并在这种情况下返回true。

Is this possible with JavaScript? JavaScript有可能吗?

Try: 尝试:

/^(?!.*foobar)/.test('foobar@bar.de')

A (short) explanation: (简短)说明:

^          # start of the string 
(?!        # start negative look-ahead
  .*       # zero or more characters of any kind (except line terminators)
  foobar   # foobar
)          # end negative look-ahead

So, in plain English, that regex will look from the start of the string if the string 'foobar' can be "seen". 因此,用简单的英语来说,如果可以“看到”字符串“ foobar”,则该正则表达式将从字符串开头开始。 If it can be "seen" there is no * match. 如果可以“看到”,则没有 *匹配项。

* no match because it's negative look-ahead! *不匹配,因为它是负面的超前预测!

More about this look-ahead stuff: http://www.regular-expressions.info/lookaround.html But Note that JavaScript only supports look-aheads, no look- behinds ! 更多关于这个先行的东西: http://www.regular-expressions.info/lookaround.html但要注意的JavaScript仅支持查找aheads,没有look- 屁股

^(?!.*(word1|word2|word3))

will match a string that does not contain any of word1 , word2 , or word3 (and you can extend the list indefinitely). 将匹配不包含word1word2word3任何一个的字符串(并且您可以无限期扩展列表)。 But this also matches null strings. 但这也匹配空字符串。 To reject nulls use 拒绝null使用

^(?!$)(?!.*(word1|word2|word3))  

Here's an example of an inequality. 这是一个不平等的例子。 First I isolate the operator '<', later the operands 'a' and 'b'. 首先,我将运算符“ <”隔离开,然后将操作数“ a”和“ b”隔离开。 Basically, I take the direct expression, include it into right parentheses, invert the latter by '^' and finally embed the resulting expression into square brackets, 'cause the '^' at the beginning would be interpreted differently. 基本上,我使用直接表达式,将其包含在右括号中,将其用'^'反转,最后将结果表达式嵌入方括号中,因为'开头的'^'的解释会有所不同。

var _str = "a < b" ;
var _op = /</g ;
var _no_op = /[^(<|\ )]/g ;
console.log( _str, _str.match( _op ) ); // get '<'
console.log( _str, _str.match( _no_op ) ); // get 'a', 'b'

Ps: I just added the blank space in the inverse expression, in order to retrieve exact matching for the operands. Ps:我只是在逆表达式中添加了空格,以便检索操作数的精确匹配。

If what you're searching for really isn't more complicated than a simple string like "foobar": 如果您要搜索的内容实际上并不比“ foobar”之类的简单字符串复杂:

if (yourString.indexOf("foobar") === -1) {
  // ...
}

http://www.w3schools.com/jsref/jsref_indexOf.asp http://www.w3schools.com/jsref/jsref_indexOf.asp

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

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