Regex for six characters with at least one digit - javascript

I am looking for a regex with at least 6 characters (no limit) including at least one digit. No spaces allowed.
I have this this regex:
^(?=.*\d).{4,8}$
However, I don't want to limit to 8 characters.

a regex with at least 6 characters (no limit) including at least one digit. no spaces allowed.
^(?=\D*\d)\S{6,}$
Or
^(?=\D*\d)[^ ]{6,}$
See demo
^ Start of string
(?=\D*\d) - Must be 1 digit (the lookahead is based on the principle of contrast)
\S{6,} - 6 or more non-whitespaces
OR
[^ ]{6,} - 6 or more characters other than literal normal space
To enable the regex to match more than 6 characters, you only need to adjust the quantifier. See more about limiting quantifiers here.

^(?=.*\d)([\w]{6,})$
I think it should work fine

Related

Javascript: Regex to exclude whitespace and special characters

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

How to write regex to match password with rules

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!

Issue with javascript regex not matching less than 3 characters

I have the following javascript regex:
/^[^\s][a-z0-9 ]+[^\s]$/i
I need to allow any alphanumeric character as well as spaces inside the string but not at the beginning nor at the end.
Oddly enough, the above regex will not accept less than 3 characters, e.g. aa will not match but aaa will.
I am not sure why. Can anyone please help ?
You have: [^\s] (requires matching at least one non-whitespace character), [a-z0-9 ]+ (requires matching at least one alphanumeric or space character), and [^\s] again (requires matching at least one non-whitespace character). So, in total, you need at least 3 characters in the string.
Use word boundaries at the beginning and end instead:
/^\b[a-z0-9 ]+\b$/i
https://regex101.com/r/2GhH3N/1
Try the following regex:
^(?! )[a-z0-9 ]*[a-z0-9]$
Details:
^(?! ) - Start of the string and no space after it (so here we exclude the
initial space).
[a-z0-9 ]* - A sequence of letters, digits and spaces, possibly empty
(the content before the last letter(see below).
[a-z0-9]$ - The last letter and the end of string (so here we exclude the
terminal space).
You should re-write the expression as
/^[a-z0-9]+(?:\s+[a-z0-9]+)*$/i
See the regex demo.
NOTE: If only one whitespace is allowed between the alphanumeric chars use
/^[a-z0-9]+(?:\s[a-z0-9]+)*$/i
^^
Details
^ - start of string
[a-z0-9]+ - 1+ letters/digits
(?:\s+[a-z0-9]+)* - 0 or more repetitions of 1+ whitespaces (\s+) and 1+ digit/letters
$ - end of string.
See the regex graph:

Want this regex work for min 8 characters. No Max Limit

My Regex: ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$#$!%*?&])
It accepts at least 1 lowercase letter, 1 uppercase letter, 1 number and 1 special character.
I want this to work for minimum 8 characters. Should not match if string length less than 8.
I have tried (^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$#$!%*?&])){8,} this. But it still accepts lengths less than 8 Abc#123.
You appended the limiting quantifier to a capture group 1 (around the whole pattern) meaning you want to repeat the lookahead checks 8 or more times.
Either add one more lookahead:
/^(?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[#$!%*?&])/
^^^^^^^^^
See the regex demo.
Alternatively, you may add .{8,} at the end
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[#$!%*?&]).{8,}/
^^^^^
See this regex demo.
And no need to repeat the $ character inside the character class ([$#$!%*?&] -> [#$!%*?&]), unless you meant something else.
The lookahead at the start variation may turn out preferable in cases when the string won't match due to its length.
console.log(/^(?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[#$!%*?&])/.test("1sD$"))
console.log(/^(?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[#$!%*?&])/.test("1sD$2sD$"))
Minimum 8 characters at least 1 Alphabet and 1 Number:
"^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$"
Minimum 8 characters at least 1 Alphabet, 1 Number and 1 Special Character:
"^(?=.*[A-Za-z])(?=.*\d)(?=.*[$#$!%*#?&])[A-Za-z\d$#$!%*#?&]{8,}$"
Minimum 8 characters at least 1 Uppercase Alphabet, 1 Lowercase Alphabet, 1 Number and 1 Special Character:
"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$#$!%*?&])[A-Za-z\d$#$!%*?&]{8,}"

Regex not matching 6 repeated numbers

I am trying to get a regular expression to work but am stumped. What I want is to do the inverse of this:
/(\w)\1{5,}/
This regex does the exact opposite of what I'm trying to do. I would like to get everything but a string that has 6 repeating numbers i.e. 111111 or 999999.
Is there a way to use a negative look-around or something with this regex?
You can use this rgex:
/^(?!.*?(\w)\1{5}).*$/gm
RegEx Demo
(?!.*?(\w)\1{5}) is a negative lookaahead that will fail the match if there are 6 consecutive same word characters in it.
I'd rather go with the \d shorthand class for digits since \w also allows letters and an underscore.
^(?!.*(\d)\1{5}).*$
Regex explanation:
^ - Start of string/line anchor
(?!.*(\d)\1{5}) - The negative lookahead checking if after an optional number of characters (.*) we have a digit ((\d)) that is immediately followed with 5 identical digits (\1{5}).
.* - Match 0 or more characters up to the
$ - End of string/line.
See demo. This regex will allow

Categories