简体   繁体   English

Javascript正则表达式匹配以某些字符结尾但不与这些字符的特定组合结束的字符串

[英]Javascript regex to match a string that ends with some characters but not with a particular combination of those

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 , xxxabd but not xxxabc . 所以正则表达式应该匹配xxxaxxxbcxxxabd而不是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"))我不能做某事

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 xxx matches. 你提到你还需要字符串以[abcde]*结尾,但*表示它是可选的,所以xxx匹配。 I assume you really want [abcde]+ , which also simply means that it needs to end with [abcde] . 我假设你真的想要[abcde]+ ,这也只是意味着它需要以[abcde]结尾。 In that case, the assertions are: 在这种情况下,断言是:

(?=.*[abcde]$)(?!.*abc$)

See regular-expressions.info for 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). 我不愿意提供实际的Javascript正则表达式,因为我不熟悉该语言(尽管我确信断言,如果支持,将起作用 - 根据regular-expressions.info ,Javascript支持正面和负面的预测)。 Thanks to Pointy and Alan Moore's comments, I think the proper Javascript regex is this: 感谢Pointy和Alan Moore的评论,我认为正确的Javascript正则表达式是这样的:

var regex = /^(?!.*abc$).*[abcde]$/;

Note that this version (with credit to Alan Moore) no longer needs the positive lookahead. 请注意,此版本(归功于Alan Moore)不再需要积极的前瞻性。 It simply matches .*[abcde]$ , but first asserting ^(?!.*abc$) . 它只匹配.*[abcde]$ ,但首先断言^(?!.*abc$)

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]* /结尾

Firstly, note every string ends with [abcde]* , as that allows zero width. 首先,请注意每个字符串以[abcde]*结尾,因为它允许零宽度。 Thus we're really just looking for a regex that matches strings that don't end in abc . 因此,我们真的只是在寻找匹配不以abc结尾的字符串的正则表达式。 Easy. 简单。

([^c]|[^b]c|[^a]bc)$

That's something that's not c , something that's not b followed by c , or something that's not a followed by bc , and whichever option of those, then followed by the end of the string. 那是不是c东西,不是b后跟c东西,或者不是bc后面的东西,以及那些选项中的任何a ,然后是字符串的结尾。

Just want to say to @maček that you answer is simple and clear with ! 只想对@maček说你回答简单明了! 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::findIndex and without having to create a function or a wile loop 在开始但我真的希望not运算符在regexp中,所以我可以使用它与例如: Array::someArray::filterArray::findArray::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"] 

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"; 那么“ab”后跟“a”,“b”,“d”或“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"; 或任何两个字符的序列,其中第一个字符不是“a”或第二个字符不是“b”,后面跟着“a”到“e”; or any word boundary followed by any character (possibly) followed by "a" through "e". 或任何字边界后跟任何字符(可能)后跟“a”到“e”。 The last clause is to deal with short strings; 最后一个条款是处理短字符串; it's sort-of cheating but in this case it works. 这是一种欺骗,但在这种情况下它是有效的。

暂无
暂无

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

相关问题 javascript 检查字符串是否以某些字符开头并删除这些字符 - javascript check if string starts with some characters and remove those characters 正则表达式Javascript用前两个字符匹配字符串 - Regex Javascript Match string by first two characters JavaScript正则表达式-匹配一个包含一些字符但不包含其他字符的字符串 - JavaScript Regex - Match a string with that contains some characters and doesn't contain others 如何创建正则表达式javascript函数以匹配字符串的某些组合? - How to create regex javascript function to match certain combination of string? 正则表达式字符串以不能在 Javascript 中工作结束 - Regex string ends with not working in Javascript javascript 中的正则表达式对以某个字符串 X 结尾并包含某个字符串 Y 的字符串使用 RegExp 构造函数 - Regex in javascript using RegExp contructor for the strings that ends with some string X and contains some string Y 正则表达式以匹配以点开头和结尾的字符串 - Regex to match a string that starts and ends with a dot 用正则表达式匹配一组特定的字符串 - Match a Particular set of string with regex 如何在javascript正则表达式中的字符之间匹配字符串 - How do I match a string between characters in javascript regex 正则表达式以匹配JavaScript中某些字符未包含的字符串? - Regex to match string that's not contained in certain characters in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM