Regex, detect no spaces in the string - javascript

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,}$/

Related

How can I write the Javascript Regular Expression pattern to handle these conditions

In My exercise, I'm given a task to write a regular expression pattern that validates username input for storage into database.
Usernames can only use alpha-numeric characters.
The only numbers in the username have to be at the end. There can be zero or more of them at the end. Username cannot start with the number.
Username letters can be lowercase and uppercase.
Usernames have to be at least two characters long. A two-character username can only use alphabet letters as characters.
I succeeded to pass all tests except one
A1 is not supposed to match the patern
let userCheck = /^[A-Za-z]+\w\d*$/;
let result = userCheck.test(username);
You can use an alternation after ^[a-z] the first letter to require either [a-z]+ one or more letters followed by \d* any amount of digits | OR \d{2,} two or more digits up to $ end of the string.
let userCheck = /^[a-z](?:[a-z]+\d*|\d{2,})$/i;
See this demo at regex101 - Used with the i-flag (ignore case) to shorten [A-Za-z] to [a-z].
PS: Just updated my answer at some late cup of coffee ☕🌙. Had previously misread the question and removed my answer in meanwhile. I would also have missed the part with e.g. Z97 which I just read at the other answers comments. It's much more of a challenge than at first glance... obviously :)
Edit:
My first answer did not fully solve the task. This regex does:
^([A-Za-z]{2}|[A-Za-z]\w{2,})$
it matches either two characters, or one character followed by at least two characters and/or digits (\w == [A-Za-z0-9]). See the demo here: https://regex101.com/r/sh6UpX/1
First answer (incorrect)
This works for your description:
let userCheck = /^[A-Za-z]{2,}\d*$/;
let result = userCheck.test(username);
Let me explain what went wrong in your regex:
/^[A-Za-z]+\w\d*$/
You correctly match, that the first character is only a letter. The '+' however only ensures, that it is matched at least one time. If you want to match something an exact number of times, you can append '{x}' to your match-case. If you rather want to match a minimum and maximum amount of times, you can append '{min, max}'. In your case, you only have a lower limit (2 times), so the max stays empty and means unlimited times: {2,}
After your [2 - unlimited] letters, you want to have [0 - unlimited] numbers. '\w' also matches letters, so we can just remove it. The end of your regex was correct, as '\d' matches any digit, and '*' quantifies the previous match for the range [0 - unlimited].
I recommend using regex101.com for testing and developing regex patterns. You can test your strings and get very good documentation and explanation about all the tags. I added my regex with some example strings to this link:
https://regex101.com/r/qPmwhG/1
The strings that match will be highlighted, the others stay without highlighting.

4 or more of the same consecutive letters

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.

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.

Javascript RegExp non sequential characters

I have this rule
var reg = new RegExp('[a-z]{3}');
Which means it is allowed to use characters between a-z and at least 3 occurrences.
So, I am wondering if there is a way to match this rule with non sequential characters.
In other words,
"abc" => valid
"aaa" => not valid
Thank you!
Here is a working regex for exactly 3 (or N) characters, if the number is not fixed it gets more complicated:
^([a-z])(?!\1{2})[a-z]{2}$
1 2 3 4 5 6 7 8
Explanation:
^ matches the beginning of the string
([a-z]) match one of the accepted characters and save it (group 1)
(?!...) negative lookahead, what is in those brackets is not accepted
\1 reference to the first group (first character here)
{2} repeated exactly twice
[a-z] the accepted characters
{2} repeated exactly twice
$ matches the end of the string
Link here (I added the gm modifiers, so that several expressions can be tested.)
Try to use the excluding lookahead (?![a-z]{3}), it will not match 3 equal characters in sequence.

Regex - At least 1 number, 1 letter, 1 special character and at least 3 characters

I wanna test my own regex in order to understand how can i use it and create my custom regex.
I tried this one :
^(?=.*\d)(?=.*[a-zA-Z])(?=.*[\W_]){3}$
(?=.*\d) => at least 1 number.
(?=.*[a-zA-Z]) => at least 1 letter.
(?=.*[\W_])=> at least 1 special character.
{3} => at least 3 characters.
Unfortunately, it doesn't work, but i want at least 1 number, 1 letter and 1 special character, and at least 3 characters in my input. When the user types those 3 types of characters, my message disappears because the regex is correct ^^
Sorry for my bad english, I can give you more details if you want and thank you for the help :)
Have a nice day :)
The problem is that your {3} quantifier applies to your last look-ahead, which is nonsensical : it means that at the start of the text, you must match 3 times the look-ahead, which is a given if it matches once since lookaround are 0-width matches.
You could use the following :
^(?=.*\d)(?=.*[a-zA-Z])(?=.*[\W_]).{3,}$
which specifies, aside from your existing look-aheads, that at least 3 characters must be matched.
If you just test the string, it would also be enough to match
^(?=.*\d)(?=.*[a-zA-Z])(?=.*[\W_]).{3}
without an end anchor : you match 3 characters, and stop matching what follows since your requisites are met.
If you want 1 digit, 1 alpha, 1 special character, those are already at least 3 characters.
^(?=\D*\d)(?=.*?[a-zA-Z]).*[\W_].*$
Here's a demo at regex101. Or for only matching:
^(?=\D*\d)(?=.*?[a-zA-Z]).*[\W_]
(?=\D*\d) The first lookahead requires one digit (preceded by any \D non-digits).
(?=.*?[a-zA-Z]) second lookahead requires one alpha (preceded by any characters).
.*[\W_] matching until one special character.
All together requires at least 3 different characters: 1 digit, 1 alpha, 1 special.

Categories

Resources