4 or more of the same consecutive letters - javascript

Can anyone tell me what I can add to my existing RegEx expression so that 4 or more of the same letter consecutively is invalid? This is what I have so far:
(^[A-Za-z]{1})([A-Za-z\-\'\s]{0,})([A-Za-z]{1}$)
It meets all but 1 of my requirements so far which are:
Any alphabetic character
Single spaces but not as first or last character
Hyphens but not as the first or last character
Single quotes but not as the first or last character
No more than 3 consecutive characters the same, irrespective of case
At least 2 characters long, if present
Some examples:
James - valid
Sarah Jayne - valid
Michellle - valid
O'Brian - valid
Holly-Rose - valid
Eeeeric - invalid
Jo--anne - invalid

Based on your description, edits and comment, you can probably use this regex in Javascript:
/^(?=.{2})(?!.*([a-z])\1{3})[a-z]+(?:[' -][a-z]+)*$/gmi
RegEx Demo
There are 2 lookaheads:
(?=.{2}) - Positive lookahead to ensure there are at least 2 characters in input
(?!.*([a-z])\1{3}) Negative lookahead to ensure we don't allow 4 repeats of alphabets.

Related

Regex for String in React

How shall we write a regular expression to validate a string that should have a length of a minimum of 1 character and a maximum of 50 characters, have both upper case and lower case, alphanumeric, include space, and have mostly used special characters like #,._-&$#? The first character should be either alphabet or number then the rest can be as mentioned above.
*If it is only one character then it should be an alphanumeric one
I have tried a regex with my limited knowledge which looks like
^[a-zA-z]*[a-zA-Z\d\-_#&$%#\s]{1,50}$
But I am not able to match the string if there is only one character given, can anyone guide me to fix this
You can use
/^(?=[\p{L}0-9])[\p{L}\p{N}_#,.&$%#\s-]{1,50}$/u
See the regex demo
Details
^ - start of string
(?=[\p{L}0-9]) - the first char must be a Unicode letter (\p{L}) or an ASCII digit
[\p{L}\p{N}_#,.&$%#\s-]{1,50} - one to fifty
\p{L} - any Unicode letter
\p{N} - any Unicode digit
_#,.&$%#- - any of these chars
\s - any whitespace
$ - end of string.

Regex for No more than 2 consecutive numbers No more than 2 repeated characters?

I am looking to create a regex with conditions:
Minimum length 6
At least one number and one letter must be used
No more than 2 consecutive numbers (like 123)
No more than 2 repeated characters
What I am able to achieve
/^(?!.*([A-Za-z0-9!##$&()\\-`.+,/?"])\1{2})(?=.*[a-z])(?=.*\d)[A-Za-z0-9!##$&()\\-`.+,/?"]+$/
This validates that the string has at least one number and one letter.
Instead of consecutive numbers 123, it checks 111. and i am not able to add 4th condition in this.
Any further help will be appreciated.
Thanks in advance.
Try this Regex:
^(?=[\D]*\d)(?=[^a-zA-Z]*[a-zA-Z])(?=.{6,})(?!.*(\d)\1{2})(?!.*([a-zA-Z])(?:.*?\2){2,}).*$
Demo
Explanation:
^ - start of the string
(?=[\D]*\d) - positive lookahead - checks for the presence of a digit
(?=[^a-zA-Z]*[a-zA-Z]) - positive lookahead - checks for the presence of a letter
(?=.{6,}) - positive lookahead - checks for the presence of atleast 6 literals
(?!.*(\d)\1{2}) - Negative lookahead - Checks for the ABSENCE of 3 consecutive digits. It will allow 2 consecutive digits though. If you do not want even 2 consecutive digits, then remove {2} from this part
(?!.*([a-zA-Z])(?:.*?\2){2,}) - Negative lookahead - validates that no letter should be present more than 2 times in the string
.* - capture the string
$ - end of the string
OUTPUT:
jj112233 -Matches as it has atleast one letter, digit. Not more than 2 consecutive digits/letter. Has atleast 6 characters
jkhsfsndbf8uwwe -matches
a1234 -does not match as length<6
nsds312 -matches
111aaa222 -does not match as it has more than 2 consecutive digits and also more than 2 repeated letters
aa11bbsd -match
hgshsadh12 -does not match as it has more than 2 `h`
hh8uqweuu -does not match as it has more than 2 `u`

Regex, detect no spaces in the string

This is my current regex check:
const validPassword = (password) => password.match(/^(?=.*\d)(?=.\S)(?=.*[a-zA-Z]).{6,}$/);
I have a check for at least 1 letter and 1 number and at least 6 characters long. However I also want to make sure that there are no spaces anywhere in the string.
So far I'm able to enter in 6 character strings with spaces included :(
Found this answer here, but for some reason in my code it's passing.
What is the regular expression for matching that contains no white space in between text?
It seems you need
/^(?=.*\d)(?=.*[a-zA-Z])\S{6,}$/
Details
^ - start of string
(?=.*\d) - 1 digit (at least)
(?=.*[a-zA-Z]) - at least 1 letter
\S{6,} - 6 or more non-whitespace chars
$ - end of string anchor
With a principle of contrast in mind, you may revamp the pattern into
/^(?=\D*\d)(?=[^a-zA-Z]*[a-zA-Z])\S{6,}$/

Issues in password regular expression

Hi all I am making a password regular expression in javascript test() method, It will take the following inputs
solution
/^(?=.*\d)^(?=.*[!#$%'*+\-/=?^_{}|~])(?=.*[A-Z])(?=.*[a-z])\S{8,15}$/gm
May contains any letter except space
At least 8 characters long but not more the 15 character
Take at least one uppercase and one lowercase letter
Take at least one numeric and one special character
But I am not able to perform below task with (period, dot, fullStop)
(dot, period, full stop) provided that it is not the first or last character, and provided also that it does not appear two or more times consecutively.
Can anyone one help me to sort out this problem, Thanks in advance
You may move the \S{8,15} part with the $ anchor to the positive lookahead and place it as the first condition (to fail the whole string if it has spaces, or the length is less than 8 or more than 15) and replace that pattern with [^.]+(?:\.[^.]+)* consuming subpattern.
/^(?=\S{8,15}$)(?=.*\d)(?=.*[!#$%'*+\/=?^_{}|~-])(?=.*[A-Z])(?=.*[a-z])[^.]+(?:\.[^.]+)*$/
See the regex demo
Details:
^ - start of string
(?=\S{8,15}$) - the first condition that requires the string to have no whitespaces and be of 8 to 15 chars in length
(?=.*\d) - there must be a digit after any 0+ chars
(?=.*[!#$%'*+\/=?^_{}|~-]) - there must be one symbol from the defined set after any 0+ chars
(?=.*[A-Z]) - an uppercase ASCII letter is required
(?=.*[a-z]) - a lowercase ASCII letter is required
[^.]+(?:\.[^.]+)* - 1+ chars other than ., followed with 0 or more sequences of a . followed with 1 or more chars other than a dot (note that we do not have to add \s into these 2 negated character classes as the first lookahead already prevalidated the whole string, together with its length)
$ - end of string.

Regular expression for alphanumeric with atleast one digit

I am trying to match an alphanumeric string with at least one digit.
The second condition is that it's minimum length should be 3.
For example, the following strings should match
111
12345
ABCD1
123A
11AA11
And the following should not match
ABCD
AB
12
1A
I have got myself to a point where I can get the first condition right.
That is, having minimum one digit:
([a-zA-z0-9]*[0-9]+[a-zA-z0-9]*)
But I don't have any idea to specify a minimum length. If I try using {3},
it will require minimum of 3 numbers.
Try using a positive lookahead to ascertain that there's at least one digit, and use {3,} to indicate that it should match at least 3 characters:
/^(?=.*\d)[a-z\d]{3,}$/i
You can use lookahead to assure your expression contains a digit, and then match the minimum-three-chars:
/^(?=.*?\d)[a-zA-Z0-9]{3,}$/

Categories

Resources