brentonstrine
brentonstrine

Reputation: 22752

Regular expression to match anything that isn't the string "ABCD"

How do I write a Javascript regular expression that matches everything except a given string ("ABCD")?

Something like /[^ABCD]/ except I don't want to match everything that isn't the letter A, B, C or D. I want to match everything that isn't the string "ABCD".

Basically I want this to happen:

var myStr = "ABCA ABCB ABCD BCD ABC"
myStr.replace(/!(ABCD)/g,'') // returns ABCD

Upvotes: 3

Views: 1835

Answers (4)

alinsoar
alinsoar

Reputation: 15793

This is a direct expansion of the negation , without using a negation operator:

/^\([^a]\|a[^b]\|ab[^c]\|abc[^d]\)*$\|^a$\|^ab$\|^abc$/

When you call a negation operator into some language, it transform the expression you want to negate into something like the upper form .


Here is how I tested it:

@ThinkPad-T420:~$ E='/^\([^a]\|a[^b]\|ab[^c]\|abc[^d]\)*$\|^a$\|^ab$\|^abc$/ p;d'
@ThinkPad-T420:~$ echo abc | sed "$E"
abc
@ThinkPad-T420:~$ echo abcd | sed "$E"
@ThinkPad-T420:~$ echo abcde | sed "$E"
@ThinkPad-T420:~$ echo axbcde | sed "$E"
axbcde
@ThinkPad-T420:~$ echo xxabcde | sed "$E"
@ThinkPad-T420:~$ 

Upvotes: 0

Alan Moore
Alan Moore

Reputation: 75232

Okay, I had misinterpreted the question. It seems you want to test for the presence of ABCD, and if you find it, replace the whole string with just that: ABCD. This will do that:

s = s.replace(/.*(ABCD).*/, '$1');

But it will leave the string unchanged if there's no ABCD in it. If you want to delete the string in that case, you have to make the capture optional. But then you have to alter the first part of the regex to make it "sneak up" on the capture:

s = s.replace(/^(?:(?!ABCD).)*((?:ABCD)?).*$/, '$1');

That forces it to try to capture ABCD at every position. (It also slows things down massively--not an issue in this case, but something to keep in mind if you use this technique on large inputs.)

So a pure-regex solution does exist, but I like @bažmegakapa's solution better. :D


original answer:

/^(?!ABCD$).*/

Note that this will also match an empty string. If you have any positive requirements, you can change the .* to whatever you need. For example, to match one or more uppercase ASCII letters but not the exact string ABCD, you can use:

/^(?!ABCD$)[A-Z]+$/

Upvotes: 3

godspeedlee
godspeedlee

Reputation: 672

try with this:

var myStr = "ABCA ABCB ABCD BCD ABC";
var foo = myStr.replace(/\b(?!ABCD\b)\w+\b/g,'').trim(); // returns ABCD
document.write(foo);

output: ABCD

or:

var myStr = "ABCA ABCB ABCD BCD ABCD ABC";
var foo = myStr.replace(/\b(?!ABCD\b)\w+\b/g,'').replace(/\s/g, '');
document.write(foo);

output: ABCDABCD

Upvotes: 0

kapa
kapa

Reputation: 78691

You can simply check for ABCD, check how many of it exists in the string, then construct a new string from it like this (you can use space as a separator if it fits your case better):

var res = myStr.match(/ABCD/g);
var str = res ? res.join('') : '';

The ternary is there because match() will return null if it finds nothing - which does not have a join() method.

Upvotes: 5

Related Questions