Regex match if a substring is NOT found
I am trying to make a regex match only when the substring 'password' is not contained in the string.
The only solution that I have come across that claims to solve this is:
((?!password).)
however this matched 'password123'
Is this possible with regex? I have a feeling I might have to fall back on coding the logic.
EDIT: I am using Javascripts pattern.test(string)
You really don't need a regex here...
if (str.indexOf("password") === -1) {
// use str
}
Simple: you need to anchor the regex, and maybe add a quantifier...
^((?!password).)+$
The $
is not entirely necessary, but better have it than not.
Your regex might be looking for matches anywhere in the string, which won't work if the part it's checking is after the p
in password
.