Possible Duplicate:
Regular expression to match string not containing a word?
How can I invert a regular expression in JavaScript?
Say I have the regex foo123
. How do I match everything that is not foo123?
Possible Duplicate:
Regular expression to match string not containing a word?
How can I invert a regular expression in JavaScript?
Say I have the regex foo123
. How do I match everything that is not foo123?
Use negative lookahead for this.
(?!foo123).+
matches any string except foo123
If you want to match empty string also, use (?!foo123).*
In your case (according to the comment) the required regex is (?!P[0-9]{1,}).+
.
It matches P and 123, but not P123.
(?!foo123).+
and (?!foo123).*
regex101.com/r/gC0xA6 Add ^
and $
for validation regex101.com/r/pS3zM5 Checking that the whole string does not follow the pattern can be done by using negation to result of match function instead of negating inside the regex –
Islas foo123khsdkfh
should not be matched and afoo123khsdkfh
should be matched? –
Tavi ^
and $
) can only be used to validate that the whole string doesn't match the pattern, then I will remove the downvote. –
Islas afoo123khsdkfh
but not foo123khsdkfh
–
Tavi \b(foo123)\b|.
REPLACE BY: (?{1}\1)
–
Stoops © 2022 - 2024 — McMap. All rights reserved.
^P[0-9]{1,}$
– Gerda