JavaScript regular expression for business name with some validation - javascript

I need JavaScript regular expression for business name with following conditions:
Numbers, spaces and the following characters are allowed:
~ ` ? ! ^ * ¨ ; # = $ % { } [ ] | /. < > # “ - ‘
Should be at least 2 characters one of which must be alpha or numeric character
No preceding or trailing spaces
examples: Test1, Ta, A1, M's, 1's, s!, 1!
I tried following (for time being I used only 3 special characters for testing purpose):
^(?=(?:[^\A-Za-z0-9]*[\A-Za-z0-9]){2})[~,?,!]*\S+(?: \S+){0,}$
But it doesn't validate s! or 1!.

You can use the following to validate:
^(?!\s)(?!.*\s$)(?=.*[a-zA-Z0-9])[a-zA-Z0-9 '~?!]{2,}$
And add all the characters tht you want to allow in [a-zA-Z0-9 '~?!]
See DEMO
Explanation:
^ start of the string
(?!\s) lookahead assertion for don't start with space
(?!.*\s$) lookahead assertion for don't end with space
(?=.*[a-zA-Z0-9]) lookahead assertion for atleast one alpha or numeric character
[a-zA-Z0-9 '~?!] characters we want to match (customize as required)
{2,} match minimum 2 and maximum any number of characters from the previously defined class
$ end of the string

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

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}$'

JavaScript RegEX explicit character matches only once

I need a regular expression that has the following constraints:
Contains only [A-Z0-9.*] characters
Is 1 to 15 characters in length
Must contain a * but only once.
Therefore, the following assertions:
ABC.123 invalid (no stars)
ABC.123* valid (star at end)
*ABC.123 valid (star at beginning)
ABC.*123 valid (star in the middle)
*ABC.123* invalid (more than one star)
My goal was to only have a single expression. I could obviously have an expression that asserts the first two constraints: [A-Z0-9.*]{1,15} and then a second expression to assert the third constraint \*{1}.
Is it possible to have an expression that essentially validates the overall structure of a string input, but then rewinds and re-evaluates the string with another expression?
Using a lookahead you can use this regex:
^(?=.{1,15}$)[A-Z0-9.]*\*[A-Z0-9.]*$
RegEx Demo
RegEx Breakup:
^: Start
(?=.{1,15}$): Positive lookahead to assert that we have 1 to 15 characters in input
[A-Z0-9.]*: Match zero or more [A-Z0-9.] characters
\*: Match an asterisk literally
[A-Z0-9.]*: Match zero or more [A-Z0-9.] characters
$: End

Hyphen and Underscore should never be continuous but can be present in the string more than once

I have a requirement, where i have to validate a field in Excel.
Validations:
Field should start and end with [a-zA-Z0-9] but not with any special chars [-_]
It cannot contain "-" and "_" continuously more than once.
Example:
A--Badasd (Not allowed)
A__Bsdasdas (Not allowed)
A-_fdsfdsd (Not Allowed)
A_-sfsdfsdf (Not allowed)
A-B-adf (allowed)
A_b_adads (allowed)
I came up with this following regex, however, it doesn't seem to accept even the non-continuous entries of "-" and "_".
^[a-zA-z0-9]+(([\xFF01-\xFF5E]+|[\\-\\_])+)[a-zA-Z0-9]+$
[\xFF01-\xFF5E] is to not allow any double width characters, so please ignore it as it is working fine.
Any help would be greatly appreciable.
I can only suggest a lookahead based pattern (as [\xFF01-\xFF5E] matches _ and restricting it in JS regex will make the pattern more cumbersome):
/^[a-z0-9](?:(?!.*?[-_]{2})[\xFF01-\xFF5E-]*[a-z0-9])?$/i
See the regex demo.
This pattern will match strings of 1 char length, too, and will only match those strings starting and ending with an ASCII alphanumeric char and not having --, _-, -_ and __ in them.
If you want to "block" strings of length 1, i.e. set the minimum match length to 2, you should remove (?: and )? from the pattern above:
/^[a-z0-9](?!.*?[-_]{2})[\xFF01-\xFF5E-]*[a-z0-9]$/i
Details
^ - start of string
[a-z0-9] - an alphanumeric ASCII char
(?:(?!.*?[-_]{2})[\xFF01-\xFF5E_-]*[a-z0-9])? - an optional (1 or 0 occurrences) sequence of:
(?!.*?[-_]{2}) - a lookahead check that will fail the match if there are 2 consecutive - or _ anywhere after any 0+ chars other than line break chars
[\xFF01-\xFF5E-]* - any char in the \xFF01-\xFF5E range or/and -
[a-z0-9] - an alphanumeric ASCII char
$ - end of string.

domain name validation with custom rules

I am working on regular expression with own custom rule.
rules are the hostname must be 3-63 characters,
whole name must be 256 characters,
no special characters except dot(.), hyphen(-)
I tried this var regx = /^([A-Za-z0-9-]{3,63}?\.)+[a-zA-Z]{2,6}$/;
but the problem is the pattern is applicable to next string after dot(.). What I mean to say is
for example : "qwerty.abcde.com"
in the above "qwerty" should be 3-63 characters , but "abcde" can be any no.of characters .My pattern is applicable to next string after dot.that 3-63 rule should be applicable to only "qwerty" not to "abcde". can any one help me out.
Thanks in advance
You may use the following regex:
/^(?!.{257})[A-Za-z0-9-]{3,63}\.(?:[A-Za-z0-9-]+\.)*[a-zA-Z]{2,6}$/
See the regex demo
Details:
^ - start of string
(?!.{257}) - a negative lookahead that fails the match if the string contains 257 or more chars (other than line break chars)
[A-Za-z0-9-]{3,63} - 3 to 63 alphanumeric and - chars
\. - a dot
(?:[A-Za-z0-9-]+\.)* - zero or more sequences of
[A-Za-z0-9-]+ - 1 or more alphanumeric and - chars
\. - a dot
[a-zA-Z]{2,6} - 2 to 6 ASCII letters
$ - end of string.
So, the negative lookahead checks for the whole string length, and the {3,63} limiting quantifier is only applied to the chunk of chars before the first ..

Categories