JavaScript Regular Expression OR Operator - javascript

Saw a challenge on Twitter so I've been working my way through it, granted I am not the best with Regular Expressions. This is what I have so far:
var pass_regex = new RegExp(/^[a-z][A-Z][0-9]|[!##$%^&*()_]+$/);
I am trying to match a password input that contains:
1 Lowercase Letter
1 Uppercase Letter
1 Digit OR Special Character
Where I am getting stuck is on the 'OR' part, I thought the pipe separator between [0-9] and my set of special characters would work but it doesn't seem to. Trying to better understand how you would use regular expressions to to check for 1 Digit OR 1 Special Character. Thank you in advance for any help provided.

Atleast one:
You need to use a positive lookahead based regex for checking multiple conditions.
^(?=.*?[A-Z])(?=.*?[a-z]).*?[\W\d].*
OR
^(?=.*?[A-Z])(?=.*?[a-z]).*?[!##$%^&*()_\d].*
(?=.*?[A-Z]) Asserts that there must be atleast one uppercase letter.
(?=.*?[a-z]) Atleast one lowercase letter.
.*? non-greedy match of any character zero or more times.
If the above conditions are satisfied then match that corresponding string and also the string must contain atleast a single character from the given list [!##$%^&*()_\d] . \d in this list matches any digit character.
.* matches the following zero or more characters.
DEMO

Related

Number or special character requirement addition to password validation regex

I need to validate password with: At least one uppercase, at least one lowercase, at least one number OR symbol, at least 8 characters.
I have this regex:
/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9]).{8,20}$/
This works fine except of > it checks string on number AND symbol, but not on number OR symbol. And also character length 8-20, not at least 8 but gives range. I want it to check number OR symbol. Any ideas? Thanks and have a good day!
The (?=.*\d) positive lookahead requires a digit in the string AND (?=.*[^a-zA-Z0-9]) requires a char other than ASCII letter or digit.
To make the regex require a digit OR a char other than ASCII letter or digit, merge the two lookaheads as
(?=.*[^A-Za-z])
Basically, you need to remove 0-9 from the second lookahead and it will require any char but an ASCII letter.
Result:
/^(?=.*[^A-Za-z])(?=.*[a-z])(?=.*[A-Z]).{8,20}$/
Or, a much more efficient version based on the contrast principle:
/^(?=[A-Za-z]*[^A-Za-z])(?=[^a-z]*[a-z])(?=[^A-Z]*[A-Z]).{8,20}$/
See the regex demo.
If a space is not special, add it to the lookahead:
/^(?=[A-Za-z ]*[^A-Za-z ])(?=[^a-z]*[a-z])(?=[^A-Z]*[A-Z]).{8,20}$/
^ ^

Difficult regular expression for name validation

I'm trying to write a regular expression to check whether or not a proposed name is valid in a gaming platform.
Rules:
Name must contain at least 3 and no more than 20 letters
Name must start with a uppercaseletter
Name must never have two uppercase letters in a row
Spaces are allowed, but must be preceded by a letter and be followed by an uppercase letter
Hyphens are allowed, but must be preceded by a letter and be followed by a lowercase letter
All uppercase letters must be followed by a lowercase letter unless they are followed by a space or hyphen
I know I can check separately for the length of the string so the first rule is irrelevant, but I figured I'd list it for good measure.
Test cases (Pass):
Foo
Hello World
Hello-world
Bigsby Platt-slatt
Test cases (Fail):
foo
Hello world
Hello-World
33333333333
What regular expression can I use to solve this? Is it reasonable to expect to do this using only regular expressions, or will the pattern need to be analyzed using a different method?
Thanks
This is a possible regular expression:
(?!.*[A-Z]{2})(?!.*[^A-Za-z][ -])(?!.* ([^A-Z]|$))(?!.*-([^a-z]|$))^[A-Z].{2,19}$
See demo on regex101.com.
Explanation:
Several of the rules can be expressed as "cannot contain" kind of rules, and they are easy to implement with negative look-ahead ((?! ... )):
No two capitals in sequence:
(?!.*[A-Z]{2})
No non-letter followed by either a space or hyphen:
(?!.*[^A-Za-z][ -])
No space that is followed by a non-capital or end of string ($):
(?!.* ([^A-Z]|$)
No hyphen followed by a non-lowercase or end of string:
(?!.*-([^a-z]|$))
Finally, the actual match is done with this: a capital followed by 2 - 19 characters:
^[A-Z].{2,19}$

Why does this simple JavaScript password regex not work?

I am building a password checker - I have a simple requirement for at least three lowercase non-consecutive letters for now and either I have a large misunderstanding of regexes, or something else.
I have written the following code:
var password = 'mYpAsSwOrD',
r = new RegExp('[a-z]{3,}', 'g');
console.log(password.match(r)); // null
console.log(r.test(password)); // false
Also, is the 'g' flag needed? Does the quantifier not provide the same functionality effectively?
What is the better comparison? Matching the regex against the string (first example); or testing the string against the regex (second example)?
It matches only the strings which has atleast 3 lowercase letters.
> /^(?:[^a-z]*[a-z]){3}/.test("mYpAsSwOrD")
true
Explanation:
^ Asserts that we are at the start.
(?..) Called non-capturing groups, which won't capture any characters but would do only matching operation.
[^a-z]* this would match any character zero or more times.
[a-z] this would match a lowercase letter.
(?:[^a-z]*[a-z]){3} So if the whole non-capturing group is repeated three times, it could match the strings with atleast three lowercase letters. For testing purposes only , we don't need to go for a full match.
OR
You could try the below positive lookahead.
^(?=(?:[^a-z]*[a-z]){3})
Code:
> /^(?=(?:[^a-z]*[a-z]){3})/.test("mYpAsSwOrD")
true
(?=(.*?[a-z]){3}).*
You can try this.See demo.
http://regex101.com/r/hQ1rP0/24
Your original regex matches where the three desired characters are consecutive. You can change the regex to this:
r = /[a-z](?:[^a-z]*[a-z]){2}/;
[a-z] Matches a lowercase character.
(?:[^a-z]*[a-z]) Skips over all non-lowercase characters and attempts to match a lowercase character.
{2} Two times.
As for your other question, the g flag is not necessary as it allows repeat matches, while for your use case you're simply matching the first collection and asserting true for return.

Javascript regular expression

I am making form and there is only one more thing which I cant figure it out :(
I need regular expression for password which must be at least 7 characters long. There can be small and big letters and must contain at least one number.
I tried
[0-9]+[a-zA-Z]){7}$
You can use lookahead:
^(?=.*\d)[a-zA-Z\d]{7,}$
(?=.*\d) is a lookahead which checks for a digit in the string. Basically, .* matches the whole string and then backtracks 1 by 1 to match a digit. If it matches a digit, the regex engine comes back to its position before match. So, it just checks for a pattern.
{7,} is a quantifier which matches previous pattern 7 to many times
^ is the beginning of a string

How to write Regular expression for minimum one character in javascript?

I have small requirement in Regular expression,here I need minimum of one letter of Alphabets and followed by numbers and special characters. I tried the following regular expressions but I'm not getting the solution.
/^[a-zA-Z0-9\-\_\/\s,.]+$/
and
/^([a-zA-Z0-9]+)$/
I need minimum of one letter of Alphabets
[a-z]+
and followed by numbers and special characters.
[0-9_\/\s,.-]+
Combined together you would get this:
/^[a-z]+[0-9_\/\s,.-]+$/i
The /i modifier is added for case insensitive matching of alphabetical characters.
Try this regex:
/^[a-z][\d_\s,.]+$/i
To clarify what this does:
^[a-z] // must start with a letter (only one) add '+' for "at least one"
[\d_\s,.]+$ // followed by at least one number, underscore, space, comma or dot.
/i // case-insensitive
You need the other character selection to be separate. I'm confused as to what "numbers and special characters" means, but try:
/^[a-z]+[^a-z]+$/i

Categories