Regex for String in React - javascript

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.

Related

Regular expression to validate a string starts with a char and contains allowed chars

I need help with my regular expression written in javascript.
I have tried using the regularExpression generator online, and the best i can come up with is the this:
^[a-z.-]{0,50}$
The expression must validate the following
String first char MUST start with a-z (no alpha)
String can contain any char in range a-z (no alpha), 0-9 and the characters dash "-" and dot "."
String can be of max length 50 chars
Examples of success strings
username1
username.lastname
username-anotherstring1
this.is.also.ok
No good strings
1badusername
.verbad
-bad
also very bad has spaces
// Thanks
Almost (assuming "no alpha" means no uppercase letters)
https://regex101.com/r/O9hvLP/3
^[a-z]{1}[a-z0-9\.-]{0,49}$
The {1} is optional, I put it there for descriptive reasons
I think this should cover what you want
^[a-z][a-z0-9.-]{0,49}$
That is starts a-z but then has 0-49 of a-z, 0-9 or .-
Live example: https://regexr.com/5k8eu
Edit: Not sure if you intended to allow upper and lowercase, but if you did both character classes could add A-Z as well!
If the . and - can not be at the end, and there can not be consecutive ones, another option could be:
^[a-z](?=[a-z0-9.-]{0,49}$)[a-z0-9]*(?:[.-][a-z0-9]+)*$
Explanation
^ Start of string
[a-z] Match a single char a-z
(?=[a-z0-9.-]{0,49}$) Assert 0-49 chars to the right to the end of string
[a-z0-9]* Match optional chars a-z0-9
(?:[.-][a-z0-9]+)* Optionally match either . or - and 1+ times a char a-z0-9
$ End of string
Regex demo

Regex for validating statement descriptor for credit card statements

I'm trying to validate a string entered by the user to be used as the statement description on the credit card statement to describe the purchase.
The requirements are:
Must be between 5 and 22 characters long
Must contain at least one letter (case doesn't matter)
Cannot contain these characters: < > \ ' "
Only ASCII characters allowed
Here's what I've got so far, which is kind of working:
/^(?=.*?[a-zA-Z])[a-zA-Z0-9]{5,22}$/gm
...in that it correctly checks the length for 5-22 characters long and checks for at least one letter. However, it disallows all special characters and diacritics instead of just the few that aren't allowed. How do I modify it to allow the other allowed characters?
You could use a positive lookahead to assert a character and a negative lookahead to assert not to match any character listed in the character class.
For Javascript you can use the case insensitive flag /i and use [a-z].
Edit: As Wiktor Stribiżew points out, to match only ASCII characters you could use [\x00-\x7F] instead of using a dot.
^(?=.*[a-z])(?!.*[<>\\'"])[\x00-\x7F]{5,22}$
^ Start of string
(?=.*[a-z]) Positive lookahead to check if there is a ASCII letter
(?!.*[<>\\'"]) Negative lookahead to check that there is not any of the chars in the character class
[\x00-\x7F]{5,22} Match any ASCII character 5 - 22 times
$ End of the string
For example:
const regex = /^(?=.*[a-z])(?!.*[<>\\'"])[\x00-\x7F]{5,22}$/gmi;
See the regex demo
You may use
/^(?=[^a-z]*[a-z])(?:(?![<>\\'"])[\x00-\x7F]){5,22}$/i
/^(?=[^a-z]*[a-z])(?![^<>\\'"]*[<>\\'"])[\x00-\x7F]{5,22}$/i
If you mean printable ASCII chars are allowed use
/^(?=[^a-z]*[a-z])(?:(?![<>\\'"])[ -~]){5,22}$/i
/^(?=[^a-z]*[a-z])(?![^<>\\'"]*[<>\\'"])[ -~]{5,22}$/i
Details
^ - start of string
(?=[^a-z]*[a-z]) - there must be at least 1 ASCII letter in the string
(?:(?![<>\\'"])[ -~]){5,22} - five to twenty-two occurrences of any printable ASCII char other than <, >, \, ' and " (if [\x00-\x7F] is used, any ASCII char other than the chars in the negated character class)
(?![^<>\\'"]*[<>\\'"]) - no <, >, \, ' and " allowed in the string
$ - end of string.

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.

writing regular expression with constraints

I am working on a JavaScript regular expression for the following condition check.
Consignment number validation details:
Cnote length :12 Varchar
First Character should be Character Upper Case
Fifth Character may be character or integer
Remaining all integer
Examples of valid strings:
C991S1234567
C30811234567
I have no idea. I have tried a simple regular expression like checking only numbers or alphabets.
I have tried something like this:
^[0-9]
It allows only integers. I do not know how to add constraints to it. Any help will be greatly appreciated.
The regex for this task relies on character classes ([...]) and limiting quantifiers ({n}). ^[0-9] only checks if the first character is a digit.
You can use the following regex:
^[A-Z][0-9]{3}[a-zA-Z0-9][0-9]{7}$
See demo
Explanation:
^ - Beginning of the string
[A-Z] - First is an uppercase English letter
[0-9]{3} - The 2nd, 3rd and 4th characters are digits
[a-zA-Z0-9] - Fifth character is either a letter or digit
[0-9]{7} - Following 7 characters are digits
$ - End of string.

Regex which accepts alphanumerics only, except for one hyphen in the middle

I am trying to construct a regular expression which accepts alphanumerics only ([a-zA-Z0-9]), except for a single hyphen (-) in the middle of the string, with a minimum of 9 characters and a maximum of 20 characters.
I have verified the following expression, which accepts a hyphen in the middle.
/^[a-zA-Z0-9]+\-?[a-zA-Z0-9]+$/
How can I set the minimum 9 and maximum 20 characters for the above regex? I have already used quantifiers + and ? in the above expression.
How would I apply {9,20} to the above expression? Are there any other suggestions for the expression?
/^[a-zA-Z0-9]+\-?[a-zA-Z0-9]+$/
can be simplified to
/^[a-z0-9]+(?:-[a-z0-9]+)?$/i
since if there is no dash then you don't need to look for more letters after it, and you can use the i flag to match case-insensitively and avoid having to reiterate both lower-case and upper-case letters.
Then split your problem into two cases:
9-20 alpha numerics
10-21 characters, all of which are alpha numerics except one dash
You can check the second using a positive lookahead like
/^(?=.{10,21}$)/i
to check the number of characters without consuming them.
Combining these together gives you
/^(?:[a-z0-9]{9,20}|(?=.{10,21}$)[a-z0-9]+-[a-z0-9]+)$/i
You can do this provided you don't want - to be present exactly in middle
/^(?=[^-]+-?[^-]+$)[a-zA-Z\d-]{9,20}$/
[^-] matches any character that is not -

Categories

Resources