admin管理员组

文章数量:1406046

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

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
Share Improve this question edited Aug 9, 2012 at 22:58 kapa 78.7k21 gold badges165 silver badges178 bronze badges asked Aug 9, 2012 at 22:39 brentonstrinebrentonstrine 22.8k25 gold badges79 silver badges122 bronze badges 8
  • Your problem can probably be solved better without regex. What are you actually trying to do? – Kendall Frey Commented Aug 9, 2012 at 22:46
  • Trying to strip everything from some text that isn't a particular string. – brentonstrine Commented Aug 9, 2012 at 22:47
  • Query: if myStr = "ABCA ABCB ABCD BCD ABC ABCD", should ABCDABCD be returned? – Paul D. Waite Commented Aug 9, 2012 at 22:50
  • @godspeedlee, I think I want to do the exact opposite of that. – brentonstrine Commented Aug 9, 2012 at 22:51
  • @brentonstrine bazmegakapa's answer does exactly that. – Kendall Frey Commented Aug 9, 2012 at 22:52
 |  Show 3 more ments

4 Answers 4

Reset to default 5

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('') : '';
  • jsFiddle Demo
  • String.match()
  • Array.join()

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

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]+$/

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

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:~$ 

本文标签: javascriptRegular expression to match anything that isn39t the string quotABCDquotStack Overflow