Let’s say using Javascript, I want to match a string that ends with [abcde]* but not with abc.
So the regex should match xxxa, xxxbc, xxxabd but not xxxabc.
I am utterly confused.
Edit: I have to use regex for some reason, i cannot do something if (str.endsWith("abc"))
The solution is simple: use negative lookahead:
This asserts that the string doesn’t end with
abc.You mentioned that you also need the string to end with
[abcde]*, but the*means that it’s optional, soxxxmatches. I assume you really want[abcde]+, which also simply means that it needs to end with[abcde]. In that case, the assertions are:See regular-expressions.info for tutorials on positive and negative 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 think the proper Javascript regex is this:
Note that this version (with credit to Alan Moore) no longer needs the positive lookahead. It simply matches
.*[abcde]$, but first asserting^(?!.*abc$).