Javascript Javascript正则表达式匹配以某些字符结尾但不以这些字符的特定组合结尾的字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2601087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript regex to match a string that ends with some characters but not with a particular combination of those
提问by nimcap
Let's say using Javascript, I want to match a string that ends with [abcde]*but not with abc.
假设使用 Javascript,我想匹配一个[abcde]*以abc.
So the regex should match xxxa, xxxbc, xxxabdbut not xxxabc.
所以正则表达式应该匹配xxxa, xxxbc,xxxabd但不匹配xxxabc。
I am utterly confused.
我完全糊涂了。
Edit:I have to use regex for some reason, i cannot do something if (str.endsWith("abc"))
编辑:出于某种原因,我必须使用正则表达式,我不能做某事if (str.endsWith("abc"))
回答by polygenelubricants
The solution is simple: use negative lookahead:
解决方案很简单:使用负前瞻:
(?!.*abc$)
This asserts that the string doesn't end with abc.
这断言字符串不以abc.
You mentioned that you also need the string to end with [abcde]*, but the *means that it's optional, so xxxmatches. I assume you really want [abcde]+, which also simply means that it needs to end with [abcde]. In that case, the assertions are:
您提到您还需要以 结尾的字符串[abcde]*,但这*意味着它是可选的,因此xxx匹配。我假设你真的想要[abcde]+,这也只是意味着它需要以[abcde]. 在这种情况下,断言是:
(?=.*[abcde]$)(?!.*abc$)
See regular-expressions.infofor tutorials on positive and negative lookarounds.
见regular-expressions.info为正负lookarounds教程。
I was reluctant to give the actual Javascript regex since I'm not familiar with the language (though I was confident that the assertions, if supported, would work -- according to regular-expressions.info, Javascript supports positive and negative lookahead). Thanks to Pointy and Alan Moore's comments, I thinkthe proper Javascript regex is this:
我不愿意给出实际的 Javascript 正则表达式,因为我不熟悉这门语言(尽管我相信断言,如果支持的话,会起作用——根据regular-expressions.info,Javascript 支持正面和负面的前瞻)。感谢 Pointy 和 Alan Moore 的评论,我认为正确的 Javascript 正则表达式是这样的:
var regex = /^(?!.*abc$).*[abcde]$/;
Note that this version (with credit to Alan Moore) no longer needs the positive lookahead. It simply matches .*[abcde]$, but first asserting ^(?!.*abc$).
请注意,此版本(归功于 Alan Moore)不再需要正向预测。它只是匹配.*[abcde]$,但首先断言^(?!.*abc$)。
回答by ma?ek
Either the question is not properly defined, or everyone is overlooking a simple answer.
要么问题没有正确定义,要么每个人都忽略了一个简单的答案。
var re = /abc$/;
!re.test("xxxa"); // pass
!re.test("xxxbc"); // pass
!re.test("xxxabd"); // pass
!re.test("xxxabc"); // fail
All of these end in /[abcde]*/
所有这些都以/[abcde]*/结尾
回答by me_and
Firstly, note everystring ends with [abcde]*, as that allows zero width. Thus we're really just looking for a regex that matches strings that don't end in abc. Easy.
首先,请注意每个字符串都以 结尾[abcde]*,因为它允许零宽度。因此,我们实际上只是在寻找一个匹配不以abc. 简单。
([^c]|[^b]c|[^a]bc)$
That's something that's not c, something that's not bfollowed by c, or something that's not afollowed by bc, and whichever option of those, then followed by the end of the string.
在这个时候,不是c,这东西不是b后面c,或者东西是不是a其次bc,无论与那些选项,再其次是字符串的结尾。
回答by Endless
Just want to say to @ma?ek that you answer is simple and clear with !in the beginning
But I really wanted the not operator to be inside the regexp so i could use it with eg: Array::some, Array::filter, Array::find, Array::findIndexand without having to create a function or a wile loop
只是想对@ma?ek 说,一开始你的回答很简单明了!但我真的希望 not 运算符位于正则表达式中,这样我就可以将它与 eg: Array::some, Array::filter, 一起使用Array::find,Array::findIndex而无需创建函数或一个诡计循环
Here is an example:
下面是一个例子:
var items = ["ja", "ka", "mf", "bg", "vb", "b"]
var regex = /^((?!a$|b$).)*$/; // match a string that doesn't end with a or b
var matched = items.filter(RegExp.prototype.test, regex);
alert(matched); // result: ["mf", "bg"]
回答by Pointy
Hmm ...
唔 ...
var regex = /(ab[abde]|[abcde])$/; // wrong
maybe? wait no; hold on:
也许?等待不; 坚持,稍等:
var regex = /(ab[abde]|([^a].|.[^b])[abcde]|\b.?[abcde])$/;
So that's "ab" followed by "a", "b", "d", or "e"; or any two-character sequence where the first character isn't "a" or the second character isn't "b", followed by "a" through "e"; or any word boundary followed by any character (possibly) followed by "a" through "e". The last clause is to deal with short strings; it's sort-of cheating but in this case it works.
所以这是“ab”后跟“a”、“b”、“d”或“e”;或任何双字符序列,其中第一个字符不是“a”或第二个字符不是“b”,然后是“a”到“e”;或任何单词边界后跟任何字符(可能)后跟“a”到“e”。最后一个子句是处理短字符串;这有点作弊,但在这种情况下它有效。

