RegEx for email validation where atleast 2 char required before # - javascript

I'm trying below RegEx which need atleast 2 characters before #
^([a-zA-Z])[^.*-\s](?!.*[-_.#]{2})(?!.\.{2})[a-zA-Z0-9-_.]+#([\w-]+[\w]+(?:\.[a-z]{2,10}){1,2})$
like
NOT ALLOWED : aa.#co.kk.pp
NOT ALLOWED : aa..#co.kk.pp
NOT ALLOWED : a.a#co.kk.pp
SHOULD ALLOWED: aa#co.kk.pp
SHOULD ALLOWED: aaa#co.kk.pp
SHOULD ALLOWED: aa.s#co.kk.pp. (atleast one char after special char and before #)
SHOULD ALLOWED: aa.ss#co.kk.pp
SHOULD ALLOWED: a#co.kk.pp
Before # only allowed special char . _ - which also not consecutively like (--) also not in beginning.
i tried below RegEx also but no luck
^[a-zA-Z)]([^.*-\s])(?!.*[-_.#]{2}).(?!.\.{2})[\w.-]+#([\w-]+[\w]+(?:\.[a-z]{2,10}){1,2})$

I would suggest keeping things simple like this:
^([a-zA-Z][\w+-]+(?:\.\w+)?)#([\w-]+(?:\.[a-zA-Z]{2,10})+)$
RegEx Demo
By no means it is a comprehensive email validator regex but it should meet your requirements.
Details:
^: Start
(: Start capture group #1
[a-zA-Z]: Match a letter
[\w.+-]+: Match 1+ of word characters or - or +
(?:\.\w+)?: Match an option part after a dot
): End capture group #1
#: Match a #
(: Start capture group #2
[\w-]+: Match 1+ of word characters or -
(?:\.[a-zA-Z]{2,10})+: Match a dot followed by 2 to 10 letters. Repeat this group 1+ times
): End capture group #2
$: End

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

Regex match any js number

So as an exercise I wanted to match any JS number. This is the one I could come up with:
/^(-?)(0|([1-9]\d*?|0)(\.\d+)?)$/
This however doesn't match the new syntax with underscore separators (1_2.3_4). I tried a couple of things but I couldn't come up with something that would work. How could I express all JS numbers in one regex?
For the format in the question, you could use:
^-?\d+(?:_\d+)*(?:\.\d+(?:_\d+)*)?$
See a regex demo.
Or allowing only a single leading zero:
^-?(?:0|[1-9]\d*)(?:_\d+)*(?:\.\d+(?:_\d+)*)?$
The pattern matches:
^ Start of string
-? Match an optional -
(?:0|[1-9]\d*) Match either 0 or 1-9 and optional digits
(?:_\d+)* Optionally repeat matching _ and 1+ digits
(?: Non capture group
\.\d+(?:_\d+)* Match . and 1+ digits and optionally repeat matching _ and 1+ digits
)? Close non capture group
$ End of string
See another regex demo.
how about this?
^(-?)(0|([1-9]*?(\_)?(\d)|0|)(\.\d+)?(\_)?(\d))$

How to match a filename pattern in JavaScript?

How can i match a filename, which is exactly (Capitals included) in the following format/pattern:
yymmdd_Name1_Data_Prices,
yymmdd_Name1_Data_Contact,
yymmdd_Name1_Data_Address.
I have files that need to be uploaded and the filenames are saved in a database. I want to match the given filename, with the pattern from the database, but i am unsure how to do that.
You could use the following regular expression.
\b\d{6}(?:_[A-Z][a-z]+){3}\b
Demo
Javascript's regex engine performs the following operations.
\b # match word break
\d{6} # match 6 digits
(?: # begin non-capture group
_[A-Z][a-z]+ # match '_', one upper-case letter, 1+ lower-case letters
) # end non-capture group
{3} # execute non-capture group 3 times
\b # match word break
Match the first 6 characters, which corresponds to a date, could be more precise than simply matching 6 digits. For example, assuming the year is 2000-2020, one could replace \d{6} with
(?:[01]\d|20)(?:0[1-9]|1[0-2])(?:0[1-9]|[12]\d|30|31)
but it still does would not ensure the date is valid.

Regex that allows a single whitespace in the middle but with character limit.

Excuse my ignorance but I really need help with this, I need this regex: [A-Za-z0-9]+\s?[A-Za-z0-9]+ (an username that allows a single whitespace in the middle, but not at the beginning or at the end.), but limiting the total amount of characters to minimum 3 and maximun 30.
I have tried to adapt this answer using negative lookaheads, but so far is not working.
It has to be a regex, it can't use jQuery or anything else.
You may use a positive lookahead here:
^(?=.{3,30}$)[A-Za-z0-9]+(?:\s[A-Za-z0-9]+)?$
See the regex demo.
Details:
^ - start of string
(?=.{3,30}$) - there can be 3 to 30 chars (other than linebreak, replace the . with [A-Za-z0-9\s] to be more specific)
[A-Za-z0-9]+ - 1+ alphanumeric chars
(?:\s[A-Za-z0-9]+)? - an optional (1 or 0) occurrences of a
\s - whitespace
[A-Za-z0-9]+ - 1+ alphanumeric symbols
$ - end of string.
You can use:
(?=^[A-Za-z0-9]+\s?[A-Za-z0-9]+$).{3,30}
See a demo on regex101.com. It will match:
username123 # this one
user name 123 # this one not (two spaces!)
user name123 # this one
u sername123 # this one
username123 # this one not (space in the beginning!)

How to build a custom regex that matches dashes/alphanumeric characters and '.' dot characters that are not consecutive?

I need to build a regex that doesn't match the words with this requirements:
at least 3 characters
maximum 32 characters
only a-z0-9_-.
dots: . ok, .. nope
this is what i did:
/[0-9a-zA-Z\-\_\.]{3,32}/
the problem is that i can insert more than one . and i don't know how to fix it.
You could use the following expression:
/(?:[\w-]|\.(?!\.)){3,32}/
Explanation:
(?: - Start of a non-capturing group
[\w-] - Character set to match [a-zA-Z0-9_-]
| - Alternation, or..
\.(?!\.) - Negative lookahead to match a . character literally if it isn't followed by another . character.
) - Close the non-capturing group
{3,32} - Match the group 3 to 32 times
You may also want to add anchors if you want to match the entire string against the expression:
/^(?:[\w-]|\.(?!\.)){3,32}$/

Categories