Regular Expression allows more than specified characters - javascript

HI I am new to regular expression, I tried creation regular expression based on below conditions:
Maximum 9 characters are allowed
First character must be upper case
Ending character must be 0-9
Must contain following special character ($,%,#)
/^[A-Z][a-z0-9A-Z$#%]{3,9}(?=.*[#$%]).\d+$/
What is wrong in my regular expression?

^[A-Z](?=.*[#$%])[a-z0-9A-Z$#%]{1,7}\d$
You need to take the lookahead at the start.\d+ should be \d.{3,9} should be {1,7}

Breaking the regex down
/^[A-Z][a-z0-9A-Z$#%]{3,9}(?=.*[#$%]).\d+$/
^ # Match the start of a string
[A-Z] # First character must be a capital letter
[a-z0-9A-Z$#%]{3,9} # The next 3-9 characters must be alphanumeric or one of $, # and %.
(?=.*[#$%]) # Look-ahead, requiring that some character be one of $, # and % (note that this is strictly after the 3-9 character check)
. # Match any character
\d+ # Match one or more numeric digits
$ # Match the end of the string
Therefore a string like "Aaaa$^55555555555555555" would be matched.
You need to change your look-ahead, probably moving it to before the 3-9 character check. You'll also want to make the length of that smaller, since you're explicitly allowing a capital letter as the first character and digit as the last character, so you'll probably want to match 1-7 characters instead of 3-9.

Related

Improve JS Regex pattern for name validation

I am trying to create a regex pattern for name validation.
Application name must have the following:
Lowercase alphanumeric characters can be specified
Name must start with an alphabetic character and can end with alphanumeric character
Hyphen '-' is allowed but not as the first or last character
e.g abc123, abc, abcd-1232
This is what I got [^\[a-z\]+(\[a-z0-9\-\])*\[a-z0-9\]$][1] it doesn't work perfectly. The validation fails if you enter a single character in the field. How can I improve this pattern? Thank you in advance.
You may use the following pattern:
^[a-z](?:[a-z0-9-]*[a-z0-9])?$
Explanation:
^[a-z] starts with lowercase alpha
(?: turn off capture group
[a-z0-9-]* zero or more alphanumeric OR dash
[a-z0-9] mandatory end in alphanumeric only, if length > 1
)? make this group optional
$ end of input
You are using a negated character class [^ that matches 1 character, not being any of the specified characters in the character class.
That is followed by another character class [1] which can only match 1, so the pattern matches 2 characters, like for example #1
In your examples you seem to have only a single hyphen, so if there can not be consecutive hyphens --
^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$
Explanation
^ Start of string
[a-z] Match a single char a-z
[a-z0-9]* Optionally repeat any of a-z or 0-9
(?:-[a-z0-9]+)* Optionally repeat matching - followed by at least 1 or a-z or 0-9 (So there can not be a hyphen at the end)
$ End of string
Regex demo

jQuery RegEx. If string contains any character except

I've got this RegEx which is used to validate what the user enters
It must be a value 8 - 16 characters long and can contain ONE of the certain special characters.
/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[:;#~]).{8,16}$"
I'm not trying to show an alert if the user enters something that doesn't match the above. So a-z, A-Z, 0-9 and :;#~ are allowed but anything else shows an alert.
So Abcd1234# is OK, but if they enter Abcd1234!$ if will show the alert as ! & $ are not in the match.
I've tried adding ^ to the start of character match to try and negate them, but that didn't work.
What's the best way to do this?
It seems you only need to allow the characters mentioned in the lookaheads, create a character class with them and replace the last . with it:
/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[:;#~])[\da-zA-Z:;#~]{8,16}$/
^^^^^^^^^^^^^^
See the regex demo
The [\da-zA-Z:;#~]{8,16} pattern will match 8 to 16 chars that are either digits, ASCII letters, or :, ;, # or ~ symbols.
Details:
^ - start of string
(?=.*\d) - there must be a digit after any 0+ chars other than line break chars (a (?=\D*\d) will be more efficient as it is based on the contrast principle)
(?=.*[a-z]) - - there must be an ASCII lowercase letter after any 0+ chars other than line break chars (a (?=[^a-z]*[a-z]) will be more efficient)
(?=.*[A-Z]) - there must be an ASCII uppercase letter after any 0+ chars other than line break chars (a (?=[^A-Z]*[A-Z]) will be more efficient)
(?=.*[:;#~]) - there must be a :, ;, # or ~ after any 0+ chars other than line break chars (you may also use (?=[^:;#~]*[:;#~]))
[\da-zA-Z:;#~]{8,16} - 8 to 16 chars defined in the character class
$ - end of string.

regex for a string that can't contain # or &

Need a regular expression that contains one capital letter, 8-25 characters long, and contains one special character (excluding # and &). Here is what I have:
/^(?=.*[A-Z])(?=.*[^0-9a-zA-Z]).{8,25}$/
What do I need to add so that the regex does not accept the string if it contains a # or &?
Simply:
/^(?=.*[A-Z])(?=.*[^0-9a-zA-Z])[^#&]{8,25}$/
Explanation:
(?=.*[A-Z]) means that the match succeeds only if there is 0-n characters, followed by a capital letter.
(?=.*[^0-9a-zA-Z]) means that the match succeeds only if there is 0-n characters, followed by a character that is not 0-9, a-z or A-Z.
[^#&]{8,25} means that the main match must consist of 8-25 characters, none of which can be # or &
. will match any character apart from newline; replace it with [^#&]:
/^(?=.*[A-Z])(?=.*[^0-9a-zA-Z])[^#&]{8,25}$/

RegExp to validate the string "900 - 09 999"

What would be the correct Javascript RegExp to validate the following string "900 - 09 999"
The string should only allow digits from 0 to 9, an hyphen and a space.
Thanks in advance
Always be precise when you explain what you want your regex to validate. Are the spaces and hyphens optional or not? This stuff matters. Anyway, this validates the strict format:
"\d{3} \- \d{2} \d{3}"
And this a less strict one:
"\d{3} ?\-? ?\d{2} ?\d{3}"
If you use this:
inputField.value = inputField.value.replace(/\s*(\d\d\d)\s*-?\s*(\d\d)\s*(\d\d\d)\s*/, "$1 - $2 $3")
...it should both loosely validate AND reformat the value if it does not match perfectly.
broken down, the expression does the following:
\s* # match any amount of whitespace
(\d\d\d) # capture three digits
\s* # match any amount of whitespace
-? # match an optional hyphen
\s* # match any amount of whitespace
(\d\d) # capture two digits
\s* # match any amount of whitespace
(\d\d\d) # capture three digits
\s* # match any amount of whitespace
match means to find a set of characters that matches the expression
capture means to find a match, but store the match for later use
whitespace can be spaces, tabs or carriage return characters
any amount can mean zero or more

optional characters in javascript regular expression

I am trying to build a regular expression in javascript that checks for 3 word characters however 2 of them are are optional. So I have:
/^\w\w\w/i
what I am stumped on is how to make it that the user does not have to enter the last two letters but if they do they have to be letters
You can use this regular expression:
/^\w{1,3}$/i
The quantifier {1,3} means to repeat the preceding expression (\w) at least 1 and at most 3 times. Additionally, $ marks the end of the string similar to ^ for the start of the string. Note that \w does not just contain the characters a–z and their uppercase counterparts (so you don’t need to use the i modifier to make the expression case insensitive) but also the digits 0–9 and the low line character _.
Like this:
/^\w\w?\w?$/i
The ? marks the preceding expression as optional.
The $ is necessary to anchor the end of the regex.
Without the $, it would match a12, because it would only match the first character. The $ forces the regex to match the entire string.

Categories