I have a string A and want to test if another string B is not part of it. This is a very simple regex whose result can be inverted afterwards.
我有一个字符串a,我想测试另一个字符串B是不是它的一部分。这是一个非常简单的regex,其结果可以在之后进行反转。
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中可行吗?
4 个解决方案
#1
47
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". If it can be "seen" there is no* match.
因此,在简单的英语中,如果可以“看到”字符串“foobar”,则regex将从字符串的开头查找。如果它可以被“看见”,那就没有匹配。
* 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只支持“look-ahead”,没有“look-behind”!
#2
1
^(?!.*(word1|word2|word3))
will match a string that does not contain any of word1
, word2
, or word3
(and you can extend the list indefinitely). But this also matches null strings. To reject nulls use
将匹配一个不包含word1、word2或word3的字符串(您可以无限扩展该列表)。但它也匹配空字符串。拒绝取消使用
^(?!$)(?!.*(word1|word2|word3))
#3
1
Here's an example of an inequality. First I isolate the operator '<', later the operands 'a' and '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.
这是一个不平等的例子。首先,我将运算符的<',然后操作数'a'和'b'分离。基本上,我直接表达,包括到右括号,转化后的“^”最后结果表达式嵌入方括号,”导致“^”一开始会以不同的方式解释。
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'
P.s.: I just added the blank space in the inverse expression, in order to retrieve exact matching for the operands.
注。:我只是在逆表达式中添加了空格,以便检索操作数的精确匹配。
#4
0
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
#1
47
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". If it can be "seen" there is no* match.
因此,在简单的英语中,如果可以“看到”字符串“foobar”,则regex将从字符串的开头查找。如果它可以被“看见”,那就没有匹配。
* 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只支持“look-ahead”,没有“look-behind”!
#2
1
^(?!.*(word1|word2|word3))
will match a string that does not contain any of word1
, word2
, or word3
(and you can extend the list indefinitely). But this also matches null strings. To reject nulls use
将匹配一个不包含word1、word2或word3的字符串(您可以无限扩展该列表)。但它也匹配空字符串。拒绝取消使用
^(?!$)(?!.*(word1|word2|word3))
#3
1
Here's an example of an inequality. First I isolate the operator '<', later the operands 'a' and '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.
这是一个不平等的例子。首先,我将运算符的<',然后操作数'a'和'b'分离。基本上,我直接表达,包括到右括号,转化后的“^”最后结果表达式嵌入方括号,”导致“^”一开始会以不同的方式解释。
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'
P.s.: I just added the blank space in the inverse expression, in order to retrieve exact matching for the operands.
注。:我只是在逆表达式中添加了空格,以便检索操作数的精确匹配。
#4
0
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