Is it possible to do string negation in regular expressions? I need to match all strings that do not contain the string ".."
. I know you can use ^[^\.]*$
to match all strings that do not contain "."
but I need to match more than one character. I know I could simply match a string containing ".."
and then negate the return value of the match to achieve the same result but I just wondered if it was possible.
Similarly, the negation variant of the character class is defined as "[^ ]" (with ^ within the square braces), it matches a single character which is not in the specified or set of possible characters. For example the regular expression [^abc] matches a single character except a or, b or, c.
Regular expressions are used with the RegExp methods test() and exec() and with the String methods match() , replace() , search() , and split() .
It means "Match zero or one of the group preceding this question mark." It can also be interpreted as the part preceding the question mark is optional. In above example '?' indicates that the two digits preceding it are optional. They may not occur or occur at the most once.
You can use negative lookaheads:
^(?!.*\.\.).*$
That causes the expression to not match if it can find a sequence of two periods anywhere in the string.
^(?:(?!\.\.).)*$
will only match if there are no two consecutive dots anywhere in the string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With