3

In JavaScript, is it possible to exclude matches of one regular expression (by writing another regular expression)? For example, I'd like to exclude all matches of the regular expression

/(cl|g|cr|d)(own)/ (which matches "clown", "gown", "crown", and "down")

from the regular expression

/(c|g|cl|cr|d)(o(w|u))(n|d)/ (which matches "crown", "clown", "gown", "clod", etc.).

The combined regular expression should match all strings that match the first regular expression, but not the strings that match the second regular expression.

4

1 に答える 1

4

Well, you could write it like this:

/(?:cl|g|cr|d)o(?:un|wd)|co[wu][nd]/

But more generally you could use a negative lookahead:

/(?!(?:cl|g|cr|d)own)(?:c|g|cl|cr|d)o[wu][nd]/
于 2013-03-23T01:25:37.813 に答える