This question already has answers here:
Regular expression to validate username
(12 answers)
Closed 2 years ago.
I have tried lots of regex patterns but I couldn't find a perfect regex which does the following (min. 3 char and max. 30, special characters can be allowed, if all characters are only special characters it should return false).
Expectation:
"ben":true,(3 char satisfied)
"ab":false, (3 char not satisfied)
"ben franklin":true, (space can be allowed)
"ben_franklin":true, (special char can be allowed)
"ben#:true, (special char and min 3 char satisfied)
"%#$":false, (though 3char ,but its all special char)
"ben#franklin":true,
"be'franklin":true
"&%^$##!*&^":false
If by "special characters", you mean any non-alphanumeric characters, you may use the following pattern:
^(?=.*[A-Za-z0-9]).{3,30}$
Demo.
Breakdown:
^ # Beginning of the string.
(?=.*[A-Za-z0-9]) # Must contain at least one alphanumeric (English) character.
.{3,30} # Match any character (special or not) between 3 and 30 times.
$ # End of string.
Further adjustments:
If you want to add more characters to the "not special chars", you may add them inside the character class. For example, to add the space char, you may use [A-Za-z0-9 ].
If you want to limit the "special characters" to a particular set of characters, then you may replace .{3,30} with [A-Za-z0-9##$%....]{3,30}.
I would probably just do this with 2 passes:
let str = "ben#franklin";
let result = !!(str.match(/^[a-z\s!##$%^&_-]{3,30}$/g) && str.match(/^.*[a-z].*$/g));
console.log(result);
Check for valid characters.
Make sure at least one a-z character.
You didn't mention numbers or uppercase characters, so I didn't add them.
Related
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
The objective is to match strings that are greater than 5 characters long, do not begin with numbers, and have two consecutive digits. I thought my regex was enough to do that but is not matching "bana12".
This regex does the job:
var pwRegex = /^\D(?=\w{5})(?=\w*\d{2})/;
Is not this regex more restrictive than mine? Why do I have to specify that the two or more digits are preceded by zero or more characters?
It is less restrictive than yours.
After the \D, there are 2 lookaheads. For your regex, they are
(?=\w{5})(?=\d{2})
This means that the thing after the non-digit must satisfy both of them. That is,
there must be 5 word characters immediately after the non-digit, and
there must be 2 digits immediately after the non-digit.
There is ana12 immediately after the non digit in the string. an is not 2 digits, so your regex does not match.
The working regex however has these two lookaheads:
(?=\w{5})(?=\w*\d{2})
It asserts that there must be these two things immediately after the \D:
5 word characters, and
a bunch of word characters, followed by two digits
ana12 fits both of those descriptions.
Try this Regex101 Demo. Look at step 6 in the regex debugger. That is when it tries to match the second lookahead.
You were on the right track to maybe use lookaheads, and also with the correct start of your pattern, but it is missing a few things. Consider this version:
^\D(?=.*\d{2})\w{4,}$
Here is an explanation of the pattern:
^ from the start of the string
\D match any non digit character
(?=.*\d{2}) then lookahead and assert that two consecutive digits occur
\w{4,} finally match four or more word characters (total of 5 or more characters)
$ end of the string
The major piece missing from your current attempt is that it only matches one non digit character in the beginning. You need to provide a pattern which can match 5 or more characters.
This question already has answers here:
Regex to match '-' delimited alphanumeric words
(5 answers)
Closed 4 years ago.
sub: pattern to allow an optional hyphen between 2 alphanumeric and restrict space as well.No other specialcharacters
I need a regex to match following test cases:-
"all-owed" "allowe-d" "a-llo-wed-12" "allowed12" "allow-12ed"
"allowed-12" "allo-wed-12"
"not allowed" "not allowed " " notallowed" " nota-llowed"
"not--allowed" "not#allowed"
patterns tried:
^[^-]*-?[^-]*$
^\\w+(-\\w+)*$
Regex to text a string which will have only numbers and an optional hyphen. Hyphen can be at any position after character 2
javascript regex for special characters
Java regex - alphanumeric, at most one hyphen, period or underscore, seven characters long
PHP regex to match alphanumeric and hyphens (not spaces) for use as subdomain
and some more
PLEASE POST THE DUPLICATE QUESTION LINK. We may learn from the solutions provided there and improve our search
Your second regex ^\w+(-\w+)*$ would match your cases but \w would also match an underscore.
Perhaps you are looking for a pattern that matches one or more times an alphanumberic [a-z0-9]+ and then repeats matching a dash followed by one or more times matching an alphanumeric (?:-[a-z0-9]+)*
Note that this matches only lowercase characters. You can use the case insensitive flag to match both upper and lowercase characters.
^[a-z0-9]+(?:-[a-z0-9]+)*$
const strings = [
"all-owed",
"allowe-d",
"a-llo-wed-12",
"allowed12",
"allow-12ed",
"allowed-12",
"allo-wed-12",
"notallo-wed-12_",
"not allowed",
"not allowed ",
" notallowed",
" nota-llowed",
"not--allowed",
"not#allowed"
];
let pattern = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
strings.forEach((s) => {
console.log(s + " ==> " + pattern.test(s));
});
You can use positive lookbehind and lookahead to ensure that - is between two alphanumeric characters.
^((?<=\w)-(?=\w)|\w|\d)*$
Online demo
This question already has answers here:
Regex for password must contain at least eight characters, at least one number and both lower and uppercase letters and special characters
(42 answers)
Closed 5 years ago.
Been trying all morning to figure out a regex pattern for an AD password restriction we're trying to enforce. Any ideas?
MUST have at least one lower case character ( a-z )
MUST have at least one upper case character ( A-Z )
MUST have at least one numerical character ( 0-9 )
MUST have at least one of the following special characters, but must be able to permit all:
! # # $ % ^ & * ( ) - _ + = { } [ ] | \ : ; " ' < > , . ? /
8 to 14 characters long
Can be in ANY order
I've tried about 50 combinations and the special characters part eludes me.
The one's I've found on here or online don't include the bracket special characters and a few others unfortunately.
Multiple seperate lookaheads from the start of string should work (demo)
^(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[0-9])(?=.*?[#!#$%^&*()\-_+={}[\]|\\:;"'<>,.?\/]).{8,14}$
^ # anchors to start of string
(?=.*?[a-z]) # lookahead for lowercase
(?=.*?[A-Z]) # lookahead for uppercase
(?=.*?[0-9]) # lookahead for numbers
(?=.*?[#!#$%^&*()\-_+={}[\\]|\:;"'<>,.?\/]) # lookahead for special characters
.{8,14} # the actual capture, also sets boundaries for 8-14
$ # anchors to end of string
Updated to include !, and #. Missed them in first test.
Updated to escape hyphen.
can any one help me in creating a regular expression for password validation.
The Condition is "Password must contain 8 characters and at least one number, one letter and one unique character such as !#$%&? "
^.*(?=.{8,})(?=.*[a-zA-Z])(?=.*\d)(?=.*[!#$%&? "]).*$
---
^.* : Start
(?=.{8,}) : Length
(?=.*[a-zA-Z]) : Letters
(?=.*\d) : Digits
(?=.*[!#$%&? "]) : Special characters
.*$ : End
Try this
((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[\W]).{6,20})
Description of above Regular Expression:
( # Start of group
(?=.*\d) # must contains one digit from 0-9
(?=.*[a-z]) # must contains one lowercase characters
(?=.*[\W]) # must contains at least one special character
. # match anything with previous condition checking
{8,20} # length at least 8 characters and maximum of 20
) # End of group
"/W" will increase the range of characters that can be used for password and pit can be more safe.
You can achieve each of the individual requirements easily enough (e.g. minimum 8 characters: .{8,} will match 8 or more characters).
To combine them you can use "positive lookahead" to apply multiple sub-expressions to the same content. Something like (?=.*\d.*).{8,} to match one (or more) digits with lookahead, and 8 or more characters.
So:
(?=.*\d.*)(?=.*[a-zA-Z].*)(?=.*[!#\$%&\?].*).{8,}
Remembering to escape meta-characters.
Password with the following conditions:
At least 1 digit
At least 2 special characters
At least 1 alphabetic character
No blank space
'use strict';
(function() {
var foo = '3g^g$';
console.log(/^(?=.*\d)(?=(.*\W){2})(?=.*[a-zA-Z])(?!.*\s).{1,15}$/.test(foo));
/**
* (?=.*\d) should contain at least 1 digit
* (?=(.*\W){2}) should contain at least 2 special characters
* (?=.*[a-zA-Z]) should contain at least 1 alphabetic character
* (?!.*\s) should not contain any blank space
*/
})();
You can make your own regular expression for javascript validations;
(/^
(?=.*\d) //should contain at least one digit
(?=.*[a-z]) //should contain at least one lower case
(?=.*[A-Z]) //should contain at least one upper case
[a-zA-Z0-9]{8,} //should contain at least 8 from the mentioned characters
$/)
Example:- /^(?=.*\d)(?=.*[a-zA-Z])[a-zA-Z0-9]{7,}$/
var regularExpression = new RegExp("^^(?=.*[A-Z]{"+minUpperCase+",})" +
"(?=.*[a-z]{"+minLowerCase+",})(?=.*[0-9]{"+minNumerics+",})" +
"(?=.*[!##$\-_?.:{]{"+minSpecialChars+",})" +
"[a-zA-Z0-9!##$\-_?.:{]{"+minLength+","+maxLength+"}$");
if (pswd.match(regularExpression)) {
//Success
}
can any one help me in creating a regular expression for password validation.
The Condition is "Password must contain 8 characters and at least one number, one letter and one unique character such as !#$%&? "
^.*(?=.{8,})(?=.*[a-zA-Z])(?=.*\d)(?=.*[!#$%&? "]).*$
---
^.* : Start
(?=.{8,}) : Length
(?=.*[a-zA-Z]) : Letters
(?=.*\d) : Digits
(?=.*[!#$%&? "]) : Special characters
.*$ : End
Try this
((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[\W]).{6,20})
Description of above Regular Expression:
( # Start of group
(?=.*\d) # must contains one digit from 0-9
(?=.*[a-z]) # must contains one lowercase characters
(?=.*[\W]) # must contains at least one special character
. # match anything with previous condition checking
{8,20} # length at least 8 characters and maximum of 20
) # End of group
"/W" will increase the range of characters that can be used for password and pit can be more safe.
You can achieve each of the individual requirements easily enough (e.g. minimum 8 characters: .{8,} will match 8 or more characters).
To combine them you can use "positive lookahead" to apply multiple sub-expressions to the same content. Something like (?=.*\d.*).{8,} to match one (or more) digits with lookahead, and 8 or more characters.
So:
(?=.*\d.*)(?=.*[a-zA-Z].*)(?=.*[!#\$%&\?].*).{8,}
Remembering to escape meta-characters.
Password with the following conditions:
At least 1 digit
At least 2 special characters
At least 1 alphabetic character
No blank space
'use strict';
(function() {
var foo = '3g^g$';
console.log(/^(?=.*\d)(?=(.*\W){2})(?=.*[a-zA-Z])(?!.*\s).{1,15}$/.test(foo));
/**
* (?=.*\d) should contain at least 1 digit
* (?=(.*\W){2}) should contain at least 2 special characters
* (?=.*[a-zA-Z]) should contain at least 1 alphabetic character
* (?!.*\s) should not contain any blank space
*/
})();
You can make your own regular expression for javascript validations;
(/^
(?=.*\d) //should contain at least one digit
(?=.*[a-z]) //should contain at least one lower case
(?=.*[A-Z]) //should contain at least one upper case
[a-zA-Z0-9]{8,} //should contain at least 8 from the mentioned characters
$/)
Example:- /^(?=.*\d)(?=.*[a-zA-Z])[a-zA-Z0-9]{7,}$/
var regularExpression = new RegExp("^^(?=.*[A-Z]{"+minUpperCase+",})" +
"(?=.*[a-z]{"+minLowerCase+",})(?=.*[0-9]{"+minNumerics+",})" +
"(?=.*[!##$\-_?.:{]{"+minSpecialChars+",})" +
"[a-zA-Z0-9!##$\-_?.:{]{"+minLength+","+maxLength+"}$");
if (pswd.match(regularExpression)) {
//Success
}