Regex validation - javascript

Minimum 3 characters, all small case, can use maximum of 2 numbers, no special characters allowed.
I tried using ^[a-zA-Z0-9]*$ but I'm unable to limit the numbers used
Can someone help me.

You could use a negative lookahead assertion to rule out more than 3 digits:
/^(?!(?:.*\d+){3,})[a-z0-9]{3,}$/
Here is an explanation of the pattern:
^ from the start of the string
(?!(?:.*\d+){3,}) assert that 3 or more digits do NOT occur
[a-z0-9]{3,} then match 3 or more lowercase letters or digits
$ end of the string
Here is a working demo.

You could use check if there are at least 3 allowed characters, and then match 0, 1 or 2 digits.
^(?=[A-Za-z\d]{3})[A-Za-z]*(?:\d[A-Za-z]*){0,2}$
Explanation
^ Start of string
(?=[A-Za-z\d]{3}) Positive lookahead, assert 3 allowed chars
[A-Za-z]* Match optional chars A-Za-z
(?:\d[A-Za-z]*){0,2} Repeat 0-2 times matching a single digit and optional chars A-Za-z
$ End of string
See the matches on regex101.

Related

Regex - Allow one or two alphabet and its can be at anywhere in string

I want regex which can fulfill below requirement:
Between 6 and 10 total characters
At least 1 but not more than 2 of the characters need to be alpha
The alpha characters can be anywhere in the string
We have tried this but not working as expected : (^[A-Z]{1,2}[0-9]{5,8}$)|(^[A-Z]{1}[0-9]{4,8}[A-Z]{1}$)|(^[0-9]{4,8}[A-Z]{1,2}$)|([^A-Z]{3}[0-9]{6,9})
Can anyone please help me to figure it out?
Thanks
You can assert the length of the string to be 6-10 char.
Then match at least a single char [A-Z] between optional digits, and optionally match a second char [A-Z] between optional digits.
^(?=[A-Z\d]{6,10}$)\d*[A-Z](?:\d*[A-Z])?\d*$
^ Start of string
(?=[A-Z\d]{6,10}$) Positive lookahead to assert 6-10 occurrences of A-Z or a digit
\d*[A-Z] Match optional digits and then match the first [A-Z]
(?:\d*[A-Z])? Optionally match optional digits and the second [A-Z]
\d* Match optional digits
$ End of string
See a regex demo.
One option is to use the following regular expression:
^(?=.*[a-z])(?!(?:.*[a-z]){3})[a-z\d]{6,10}$
with the case-indifferent flag i set.
Demo
This expression reads, "Match the beginning of the string, assert the string contains at least one letter, assert the string does not contain three letters and assert the string contains 6-10 characters, all being letters or numbers".
The various parts of the expression have the following functions.
^ # match the beginning of the string
(?= # begin a positive lookahead
.*[a-z] # match zero or more characters and then a letter
) # end positive lookahead
(?! # begin a negative lookahead
(?: # begin a non-capture group
.*[a-z] # match zero or more characters and then a letter
){3} # end non-capture group and execute it 3 times
) # end negative lookahead
[a-z\d]{6,10} # match 6-10 letters or digits
$ # match end of string
Note that neither of the lookaheads advances the string pointer maintained by the regex engine from the beginning of the string.

Regex to validate an alphanumeric string with optional space

I have been working on a regex for validating an alphanumeric string with the rules as below:
The first FOUR starting characters must be numbers and last
TWO characters must be alphabets.
The space is OPTIONAL but must be placed between two characters,
meaning trailing space is not allowed.
The length of postal code must be 6 characters if SPACE is
not included and 7 characters if space is included.
Eg.
1111 ZZ
111 1ZZ
1 111ZZ
1111ZZ
I tried using ^[0-9]{4}[A-Za-z]{2}$|^(?=[\d|\D]+ [\d|\D]+).{7}$ but this also validates 9999 1A as TRUE which should actually be FALSE.
Any leads or help will be appreciated :)
(?=^.{6,7}$)^(([0-9] ?){4}( ?[a-zA-Z]){2})$
will match
1111 ZZ
111 1ZZ
1 111ZZ
1111ZZ
1111 ZZ
but not
9999 1A
11111 Z
1111111
11 11 ZZ
https://regex101.com/r/lByOx6/1
EDIT: explanation
The "Positive Lookahead" part:
(?=^.{6,7}$) this only matches if the string meets the requirements, BUT it does not 'consume' the characters.
. is any character
{6,7} is about repetitions
so (?=^.{6,7}$) is matched if the string has 6 or 7 characters, no matter what
Then the following part already 'consumes' the string to say that I want at the start 4 repetitions of numbers and optionally space, and at the end 2 repetitions of letters and optionally space. The second part would accept strings such as 1 1 1 1 Z Z but as those are more than 7 characters, the first part wouldn't let the string match.
I suggest simplifying the problem ahead of time, by reducing all white spaces, which you seem to be uninterested in anyway:
var candidate = input.replaceAll(/\s/mg, '');
Then the regex is simply: /^\d{4}[A-Za-z]{2}$/
If, however, you need to validate, that there actually are no leading or trailing white spaces, you can validate that ahead of time, and return a negative result right away.
Another option is to check if the string contains an optional space between the first and the last non whitespace character.
Then match the first digit followed by 3 digits separated by an optional space and 2 or 3 times a char a-zA-Z or a space.
Using a case insensitive match:
^(?=\S+ ?\S+$)\d(?: ?\d){3}[A-Z ]{2,3}$
Explanation
^ Start of string
(?= Positive lookahead, assert what on the right is
\S+ ?\S+$ Match optional space between the first and the last non whitespace char
) Close lookahead
\d(?: ?\d){3} Match a digit and repeat 3 times an optional space and a digit
[a-zA-Z ]{2,3} Match 2-3 times either a char a-zA-Z or a space
$ End of string
Regex demo

How to create Regex to filter out results with few complex conditions regarding length, case and classes of characters

I have the following filtered:
2 digits (?=..*\d)
2 uppercase characters (?=..*[a-z])
2 lowercase characters (?=..*[A-Z])
10 to 63 characters .{10,63}$
Which translates to:
(?=.{2,}\d)(?=..*[a-z])(?=..*[A-Z]).{10,63}
Then I want to exclude a word starting with the letter u, and ending with three to six digits:
([uU][0-9]{3,6})
However, how can I merge these two patterns to do the following:
It should not allow the following because it respectively:
# does not have the required combination of characters
aaaaaaaaaaaaaaa
# is too long
asadsfdfs12BDFsdfsdfdsfsdfsdfdsfdsfdfsdfsdfsdfsdsfdfsdfsdfssdfdfsdfssdfdfsdfssdfdfsdfsdfsdfsdfsfdsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfs
# contains the pattern that shouldn't be allowed
U0000ABcd567890
ABcd56U00007890
D4gF3U432234
D4gF3u432234
ABcd567890U123456
should allow the following:
# it has the required combination of characters
ABcd5678990
ABcd567890
# does contain a part of the disallowed pattern (`([uU][0-9]{3,6})`), but does not fit that pattern entirely
ABcd567890U12
ABcd5U12abcdf
s3dU00sDdfgdg
ABcd56U007890
Created and example here: https://regex101.com/r/4b2Hu9/3
In your pattern you make use of a lookahead (?=..*\d) which has a different meaning than you assume.
It means if what is directly on the right is 2 or more times any char except a newline followed by a single digit and the same for the upper and lowercase variants.
You could update your pattern to:
^(?!.*[uU]\d{3,6})(?=(?:\D*\d){2})(?=(?:[^a-z]*[a-z]){2})(?=(?:[^A-Z]*[A-Z]){2}).{10,63}$
In parts
^ Start of string
(?!.*[uU]\d{3,6}) Negative lookahead, assert not u or U followed by 3-6 digits
(?=(?:\D*\d){2}) Assert 2 digits
(?=(?:[^a-z]*[a-z]){2}) Assert 2 lowercase chars
(?=(?:[^A-Z]*[A-Z]){2}) Assert 2 uppercase chars
.{10,63} Match any char except a newline 10-63 times
$ End of string
Regex demo
First, the way to ensure that the string contains, for example, two digits would be to use a positive lookahead:
(?=.*\d.*\d)
You can generalize this to your other filters.
To make sure the string contains 10 - 63 characters:
.{10,63}
You say you do not want the string to begin with u or U followed by 3 to 6 digits (presumbaly 7 digits is okay), use a negative lookahead:
(?![uU]\d{3,6}\D)
The \D is required to make sure that if there is a 7th digit, then the string will be accepted.
Putting it all together:
r'^(?=.*\d.*\d)(?=.*[a-z].*[a-z])(?=.*[A-Z].*[A-Z])(?![uU]\d{3,6}\D).{10,63}$'

Regex validation for mixed digits for a max of 6 characters

I need a regex validation for mixed length, a total length of 6 characters in that 4-6 characters in caps/numbers and 0-2 spaces.
I tried like
^[A-Z0-9]{4,6}+[\s]{0,2}$
but it results in a max length of 8 characters, but I need a max of 6 characters.
If the alphanumeric chars should only appear at the start of the string and the whitespaces can appear at the end (i.e. the order of the alphanumerics and whitespaces matters), you may use
/^(?=.{6}$)[A-Z0-9]{4,6}\s*$/
See the regex demo
Details
^ - start of string
(?=.{6}$) - the string length is restricted to exactly 6 non-line break chars
[A-Z0-9]{4,6} - 4, 5 or 6 uppercase ASCII letters or digits
\s* - 0+ whitespaces (but actually, only 0, 1 or 2 will be possible to add as the total length is already validated with the lookahead)
$ - end of string.
If you want to match the alphanumeric and whitespaces anywhere inside the string, you need a lookaround based regex like
^(?=(?:[^A-Z0-9]*[A-Z0-9]){4,6}[^A-Z0-9]*$)(?=(?:\S*\s){0,2}\S*$)[A-Z0-9\s]{6}$
See the regex demo
Details
^ - start of string
(?=(?:[^A-Z0-9]*[A-Z0-9]){4,6}[^A-Z0-9]*$) - a positive lookahead that requires the presence of 4 to 6 letters or digits anywhere inside the string
(?=(?:\S*\s){0,2}\S*$) - a positive lookahead that requires the presence of 0 to 2 whitespaces anywhere inside the string
[A-Z0-9\s]{6} - 6 ASCII uppercase letters, digits or whitespaces
$ - end of string.
To shorten the pattern, the second lookahead can be written as (?!(?:\S*\s){3}), it will fail the match if there are 3 whitespace chars anywhere inside the string. See the regex demo.
You can use | characters to accommodate several cases into one.
const regex = /(^[A-Z0-9]{4}\s{2}$)|(^[A-Z0-9]{5}\s$)|(^[A-Z0-9]{6}$)/g;
alert(regex.test(prompt('Enter input, including space(s)')));
If you want to match zero, one or two spaces at the end, you could use an alternation for those 3 cases.
^(?:[A-Z0-9]{4}[ ]{2}|[A-Z0-9]{5}[ ]|[A-Z0-9]{6})$
Regex demo
Explanation
^ Assert the start of the string
(?: Non capturing group
[A-Z0-9]{4}[ ]{2} Match uppercase or digit 4 times followed by 2 spaces
| Or
[A-Z0-9]{5} Match uppercase or digit 5 times followed by 1 space
| Or
[A-Z0-9]{6} Match uppercase or digit 6 times
) Close non capturing group
$ Assert the end of the string

Regex For digit count in an alphanumeric string

How can i validate following logic from regex
Allow Dollar($),Comma(,),Decimal(.) and digit(0-9)
Can have only 8 digit before Decimal irrespective of $ and comma
2 digit after decimal
Allowed string Example:
99999999.99
$99999999.99
$9,999,9999.99
$9999,99,99.99
Does not allow :
- $999999999.99 ( 9 digit before decimal)
- $99,99,999,99.99
Mean i want to restrict the count of digit only before decimal.
How can i achieve this. Thanks in Advance
You can use this regex:
/^\$?(?:,?\d){1,8}(?:\.\d{1,2})?$/gm
RegEx Demo
Explanation:
^ # Line start
\$? # match optional $ at start
(?:,?\d) # Match an optional comma followed by a digit and use non-capturing group
{1,8} # up to 8 occurrence of previous group
(?:\.\d{1,2})? # followed by optional decimal point and 1 or 2 digits
$ # line end
/^(\$?(\,?\d){1,8}\.\d{2}$)/gm
Regex Demo

Categories