The solution is simple: use negative lookahead:
(?!.*abc$)
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, so xxx
matches. I assume you really want [abcde]+
, which also simply means that it needs to end with [abcde]
. In that case, the assertions are:
(?=.*[abcde]$)(?!.*abc$)
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:
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$)
.