I'm trying to write regex to match password with following rule
Minimum length - 8 characters, if it includes a number and a lowercase letter OR 15 characters with any combination of characters
Here's my regex
^(?=.*\d[a-z]).{8,}|([a-zA-Z0-9]{15,})$
Expected result:
2232ddds - correct
ddds2222 - correct
Actual result:
2232ddds - correct
ddds2222 - incorrect
Could you help me to find a problem?
The problems are two:
The anchors only affect the alternatives they stand next to
The (?=.*\d[a-z]) lookahead requires a digit and a lowercase to appear in this order only, if there is a letter and then a digit, it won't work.
You may use
/^(?:(?=.*\d)(?=.*[a-z]).{8,}|[a-zA-Z0-9]{15,})$/
^^^|-----------------| ^ ^
See the regex demo.
If you want to make it more efficient and follow best practices use
/^(?:(?=\D*\d)(?=[^a-z]*[a-z]).{8,}|[a-zA-Z0-9]{15,})$/
Details
^ - start of string
(?: - a non-capturing group start
(?=\D*\d) - at least 1 digit
(?=[^a-z]*[a-z]) - at least 1 lowercase letter
.{8,} - any 8 or more chars other than line break chars
| - or
[a-zA-Z0-9]{15,} - 15 or more alphanumeric chars only
) - end of non-capturing group
$ - end of string.
Here a few options you can choose to build your regex:
String contains at least one:
Lower case (?=.*[a-z])
Upper case (?=.*[A-Z])
Numbers (?=.*\d)
Symbols (?=.[!##\$%\^&])
Minimal characters of string: (?=.{8,})
Your regex would be: (?=.*[a-z])(?=.*\d)(?=.{8,})|(?=.{15,})
Hope this helps!
Related
I need a regex to validate,
Should be of length 18
First 5 characters should be either (xyz34|xyz12)
Remaining 13 characters should be alphanumeric only letters and numbers, no whitespace or special characters is allowed.
I have a pattern like here, '/^(xyz34|xyz12)((?=.*[a-zA-Z])(?=.*[0-9])){13}/g'
But this is allowing whitespace and special characters like ($,% and etc) which is violating the rule #3.
Any suggestion to exclude this whitespace and special characters and to strictly check that it must be letters and numbers?
You should not quantify lookarounds. They are non-consuming patterns, i.e. the consecutive positive lookaheads check the presence of their patterns but do not advance the regex index, they check the text at the same position. It makes no sense repeating them 13 times. ^(xyz34|xyz12)((?=.*[a-zA-Z])(?=.*[0-9])){13} is equal to ^(xyz34|xyz12)(?=.*[a-zA-Z])(?=.*[0-9]), and means the string can start with xyz34 or xyz12 and then should have at least 1 letter and at least 1 digits.
You may consider fixing the issue by using a consuming pattern like this:
If you do not care if the last 13 chars contain only digits or only letters, use the patterns suggested by other users, like /^(?:xyz34|xyz12)[a-zA-Z\d]{13}$/ or /^xyz(?:34|12)[a-zA-Z0-9]{13}$/
If there must be at least 1 digit and at least 1 letter among those 13 alphanumeric chars, use /^xyz(?:34|12)(?=[a-zA-Z]*\d)(?=\d*[a-zA-Z])[a-zA-Z\d]{13}$/.
See the regex demo #1 and the regex demo #2.
NOTE: these are regex literals, do not use them inside single- or double quotes!
Details
^ - start of string
xyz - a common prefix
(?:34|12) - a non-capturing group matching 34 or 12
(?=[a-zA-Z]*\d) - there must be at least 1 digit after any 0+ letters to the right of the current location
(?=\d*[a-zA-Z]) - there must be at least 1 letter after any 0+ digtis to the right of the current location
[a-zA-Z\d]{13} - 13 letters or digits
$ - end of string.
JS demo:
var strs = ['xyz34abcdefghijkl1','xyz341bcdefghijklm','xyz34abcdefghijklm','xyz341234567890123','xyz14a234567890123'];
var rx = /^xyz(?:34|12)(?=[a-zA-Z]*\d)(?=\d*[a-zA-Z])[a-zA-Z\d]{13}$/;
for (var s of strs) {
console.log(s, "=>", rx.test(s));
}
.* will match any string, for your requirment you can use this:
/^xyz(34|12)[a-zA-Z0-9]{13}$/g
regex fiddle
/^(xyz34|xyz12)[a-zA-Z0-9]{13}$/
This should work,
^ asserts position at the start of a line
1st Capturing Group (xyz34|xyz12)
1st Alternative xyz34 matches the characters xyz34 literally (case sensitive)
2nd Alternative xyz12 matches the characters xyz12 literally (case sensitive)
Match a single character present in the list below [a-zA-Z0-9]{13}
{13} Quantifier — Matches exactly 13 times
I would like to capture a string that meets the criteria:
may be empty
if it is not empty it must have up to three digits (-> \d{1,3})
may be optionally followed by a uppercase letter ([A-Z]?)
may be optionally followed by a forward slash (i.e. /) (-> \/?); if it is followed by a forward slash it must have from one to three digits
(-> \d{1,3})
Here's a valid input:
35
35A
35A/44
Here's invalid input:
34/ (note the lack of a digit after '/')
I've come up with the following ^\d{0,3}[A-Z]{0,1}/?[1,3]?$ that satisfies conditions 1-3. How do I deal with 4 condition? My Regex fails at two occassions:
fails to match when there is a digit and a forward slash and a digit e.g .77A/7
matches but it shouldn't when there isa digit and a forward slash, e.g. 77/
You may use
/^(?:\d{1,3}[A-Z]?(?:\/\d{1,3})?)?$/
See the regex demo
Details
^ - start of string
(?:\d{1,3}[A-Z]?(?:\/\d{1,3})?)? - an optional non-capturing group:
\d{1,3} - one to three digits
[A-Z]? - an optional uppercase ASCII letter
(?:\/\d{1,3})? - an optional non-capturing group:
\/ - a / char
\d{1,3} - 1 to 3 digits
$ - end of string.
Visual graph (generated here):
This should work. You were matching an optional slash and then an optional digit from 1 to 3; this matches an optional combination of a slash and 1-3 of any digits. Also, your original regex could match 0 digits at the beginning; I believe that this was in error, so I fixed that.
var regex = /^(\d{1,3}[A-Z]{0,1}(\/\d{1,3})?)?$/g;
console.log("77A/7 - "+!!("77A/7").match(regex));
console.log("77/ - "+!!("77/").match(regex));
console.log("35 - "+!!("35").match(regex));
console.log("35A - "+!!("35A").match(regex));
console.log("35A/44 - "+!!("35A/44").match(regex));
console.log("35/44 - "+!!("35/44").match(regex));
console.log("34/ - "+!!("34/").match(regex));
console.log("A/3 - "+!!("A/3").match(regex));
console.log("[No string] - "+!!("").match(regex));
I need a regex validation for mixed length, a total length of 6 characters in that 4-6 characters in caps/numbers and 0-2 spaces.
I tried like
^[A-Z0-9]{4,6}+[\s]{0,2}$
but it results in a max length of 8 characters, but I need a max of 6 characters.
If the alphanumeric chars should only appear at the start of the string and the whitespaces can appear at the end (i.e. the order of the alphanumerics and whitespaces matters), you may use
/^(?=.{6}$)[A-Z0-9]{4,6}\s*$/
See the regex demo
Details
^ - start of string
(?=.{6}$) - the string length is restricted to exactly 6 non-line break chars
[A-Z0-9]{4,6} - 4, 5 or 6 uppercase ASCII letters or digits
\s* - 0+ whitespaces (but actually, only 0, 1 or 2 will be possible to add as the total length is already validated with the lookahead)
$ - end of string.
If you want to match the alphanumeric and whitespaces anywhere inside the string, you need a lookaround based regex like
^(?=(?:[^A-Z0-9]*[A-Z0-9]){4,6}[^A-Z0-9]*$)(?=(?:\S*\s){0,2}\S*$)[A-Z0-9\s]{6}$
See the regex demo
Details
^ - start of string
(?=(?:[^A-Z0-9]*[A-Z0-9]){4,6}[^A-Z0-9]*$) - a positive lookahead that requires the presence of 4 to 6 letters or digits anywhere inside the string
(?=(?:\S*\s){0,2}\S*$) - a positive lookahead that requires the presence of 0 to 2 whitespaces anywhere inside the string
[A-Z0-9\s]{6} - 6 ASCII uppercase letters, digits or whitespaces
$ - end of string.
To shorten the pattern, the second lookahead can be written as (?!(?:\S*\s){3}), it will fail the match if there are 3 whitespace chars anywhere inside the string. See the regex demo.
You can use | characters to accommodate several cases into one.
const regex = /(^[A-Z0-9]{4}\s{2}$)|(^[A-Z0-9]{5}\s$)|(^[A-Z0-9]{6}$)/g;
alert(regex.test(prompt('Enter input, including space(s)')));
If you want to match zero, one or two spaces at the end, you could use an alternation for those 3 cases.
^(?:[A-Z0-9]{4}[ ]{2}|[A-Z0-9]{5}[ ]|[A-Z0-9]{6})$
Regex demo
Explanation
^ Assert the start of the string
(?: Non capturing group
[A-Z0-9]{4}[ ]{2} Match uppercase or digit 4 times followed by 2 spaces
| Or
[A-Z0-9]{5} Match uppercase or digit 5 times followed by 1 space
| Or
[A-Z0-9]{6} Match uppercase or digit 6 times
) Close non capturing group
$ Assert the end of the string
I am working on regular expression with own custom rule.
rules are the hostname must be 3-63 characters,
whole name must be 256 characters,
no special characters except dot(.), hyphen(-)
I tried this var regx = /^([A-Za-z0-9-]{3,63}?\.)+[a-zA-Z]{2,6}$/;
but the problem is the pattern is applicable to next string after dot(.). What I mean to say is
for example : "qwerty.abcde.com"
in the above "qwerty" should be 3-63 characters , but "abcde" can be any no.of characters .My pattern is applicable to next string after dot.that 3-63 rule should be applicable to only "qwerty" not to "abcde". can any one help me out.
Thanks in advance
You may use the following regex:
/^(?!.{257})[A-Za-z0-9-]{3,63}\.(?:[A-Za-z0-9-]+\.)*[a-zA-Z]{2,6}$/
See the regex demo
Details:
^ - start of string
(?!.{257}) - a negative lookahead that fails the match if the string contains 257 or more chars (other than line break chars)
[A-Za-z0-9-]{3,63} - 3 to 63 alphanumeric and - chars
\. - a dot
(?:[A-Za-z0-9-]+\.)* - zero or more sequences of
[A-Za-z0-9-]+ - 1 or more alphanumeric and - chars
\. - a dot
[a-zA-Z]{2,6} - 2 to 6 ASCII letters
$ - end of string.
So, the negative lookahead checks for the whole string length, and the {3,63} limiting quantifier is only applied to the chunk of chars before the first ..
I am trying to write a regex that:
starts with a letter
contains one uppercase and one lowercase letter
contains one number
does not allow special characters
minimum 8 characters
So far I have the upper/lowercase conditions, the number and minimum character requirements set with the following regex:
/^(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[0-9]).{8,}$/
My best guess at resolving the starts with a letter and does not allow special characters requirements are below. This regex seems to evaluate all input to false:
/^[a-zA-Z](?=.*?[a-z])(?=.*?[A-Z])(?=.*?[0-9]).{8,}$/
You need to put the lookaheads after ^ and put [a-zA-Z] right after them and quantify the rest with {7,}:
^(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[0-9])[a-zA-Z][a-zA-Z0-9]{7,}$
See the regex demo.
Pattern details:
^ - start of a string
(?=.*?[a-z]) - at least 1 lowercase ASCII letter
(?=.*?[A-Z]) - at least 1 uppercase ASCII letter
(?=.*?[0-9]) - at least 1 ASCII digit
[a-zA-Z] - an ASCII letter
[a-zA-Z0-9]{7,} - 7 or more ASCII letters or digits (\w also allows _)
$ - end of string.