Javascript REGEX - javascript

I need a javascript REGEX to check that the length of the string is 9 characters. Starts with 'A' or 'a' and is followed by 8 digits.
Axxxxxxxx or axxxxxxxx

/^[aA][0-9]{8}$/ or /^[aA]\d{8}$/
Also makes sure the x's are digits :)

This should do it:
/^[aA]\d{8}$/
or
/^a\d{8}$/i

This is probably what you want.
/^([aA]\d{8})$/
The carot character means the regex must start searching from the beginning of the string, and the dollar character means the regex must finish searching at the end of the string. When they are used together it means the string must be searched from start to end.
The square brackets are used to specific a character or a range of allow characters. The slash and d means to search any digit character. The brackets at the end specify a static quantity that applies to the previous test definition. A range of quantities can be used by specifing a minimum value immediately followed by a comma immediately followed by a maximum value.

did you mean this?
/^[aA]\d{8}/
or did you mean 9 chars ?
/^[aA]\d{8}/
or did you mean A + 8 equal chars ?
/[aA](.)\1{7}/

Related

Need to write regular expression in Javascript

Need to write regular expression in javascript on a field with constraint -
The name can be up to 80 characters long. It must begin with a word character, and it must end with a word character or with ''. The name may contain word characters or '.', '-', ''."
Example -
Allowed strings -
abc.'
abc-'.'
ab-.''-a
Not allowed strings -
rish a
rish.-
What I have tried so far:
!/^[A-Za-z.-'']{1,80}$/.test(Name)
I guess, you're looking for something like this:
^(?=[A-Za-z])[A-Za-z\.\-']{0,79}[A-Za-z']$
To explain:
^(?=[A-Za-z]): Check, that the string starts with a word character. This is a look-ahead assertion, so it will NOT take a part in the match. The rest of the pattern must still account for at least 1 and max 80 characters.
[A-Za-z\.\-']{0,79}: First and middle characters, therefore max 79 chars. Minimum of one is enforced with the last character.
[A-Za-z']$: Ends with a letter or apostrophe.
Testable here: https://regex101.com/r/AOQojT/1
Using look-ahead assertion is a very clever way of solving this.
Another way would be using OR operator:
^[a-zA-Z]$|^[a-zA-Z][a-zA-Z.\-']{0,78}[a-zA-Z']$
It simply checks whether:
^[a-zA-Z]$ - there is only one word character
Or |
^[a-zA-Z]$ - one word character at the very beginning of given string
[a-zA-Z.\-']{0,78} - from zero to seventy-eight characters. . (dot) does not have to be escaped, since it has no special meaning in character set.
[a-zA-Z'] - one word character or apostrophe
Thus it validates strings longer, than 1 character.
https://regex101.com/r/CB1uOw/1

Regexp to validate ip address with last digit as non zero

I have a regex that i ended up using from one of the answer here in SO .
Basically my regex must validate ipv4 address with mask .
So i ended up using the below regex :
(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/([1-9]|1[0-9]|2[0-9]|3[0-2]|(((128|192|224|240|248|252|254)\.0\.0\.0)|(255\.(0|128|192|224|240|248|252|254)\.0\.0)|(255\.255\.(0|128|192|224|240|248|252|254)\.0)|(255\.255\.255\.(0|128|192|224|240|248|252|254))))
Now my challenge is to not allow 0 in the last digit of ip i.e ,
192.168.6.10/mask is valid but 192.168.6.0/mask is invalid
So i modified the above regexp to something like this :
(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[1][0-9][0-9]|[1-9][0-9]|[1-9]?)/([1-9]|1[0-9]|2[0-9]|3[0-2]|(((128|192|224|240|248|252|254)\.0\.0\.0)|(255\.(0|128|192|224|240|248|252|254)\.0\.0)|(255\.255\.(0|128|192|224|240|248|252|254)\.0)|(255\.255\.255\.(0|128|192|224|240|248|252|254))))
but 192.168.6.0 is always valid when testing with Angular Validators.pattern
Any idea where i'm going wrong ?
EDIT
List of IPs & its validity :
192.168.6.6/24 is valid
192.168.6.6/24 is valid
192.168.6.24/24 is valid
192.168.6.0/24 invalid
192.168.6.0/255.255.255.0 is invalid
You want to avoid matching any IP with the last octet set to 0.
You may use
ipAddress : FormControl = new FormControl('' , Validators.pattern(/^(?!(?:\d+\.){3}0(?:\/|$))(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\/(?:[1-9]|1[0-9]|2[0-9]|3[0-2]|(?:(?:128|192|224|240|248|252|254)\.0\.0\.0|255\.(?:0|128|192|224|240|248|252|254)\.0\.0|255\.255\.(?:0|128|192|224|240|248|252|254)\.0|255\.255\.255\.(?:0|128|192|224|240|248|252|254)))$/));
Here is the regex demo
The main addition is the lookahead after ^ that is executed once at the start of a string. The (?!(?:\d+\.){3}0(?:\/|$)) pattern is a negative lookahead that fails the match if, immediately to the right of the current location (string start), there are:
(?:\d+\.){3} - three repetitions of 1+ digits and a dot
0 - a zero
(?:\/|$)) - / or (|) end of string ($).
Notice I defined the pattern using a regex literal notation (/regex/) and I had to add ^ (string start) and $ (string end) anchors since the regex was no longer anchored by default. Also, to escape special chars in a regex literal notation, you only need one backslash, not two.
Suppose that the last part cannot be written 000 and 00 but just 0. Then you can you such regex
^(?:(?:2(?:5[0-5]|[0-4]\d)|1?\d?\d)\.){3}(?:(?:2(?:5[0-5]|[0-4]\d)|1?\d\d|[1-9]))$
Where diff between the first groups and the last one that one-digit value should be from 1 to 9
demo
You can try with this pattern
^(?:[1-9]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.(?:[1-9]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.(?:[1-9]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.(?:2[0-5][1-5]|[1-9]|1[0-9][1-9]|[1-9][1-9])$
Online demo
For the last numbers you have check with this
(?:2[0-5][1-5]|[1-9]|1[0-9][1-9]|[1-9][1-9])
One possible approach here is simple, and just involves adding a negative lookbehind at the very end of the pattern (?<!\.0), which asserts that .0 is not the immediately preceding term in the IP address. Applying this to your correctly working pattern from the comments above, we get:
^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}
(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\/
([1-9]|1[0-9]|2[0-9]|3[0-2]|(((128|192|224|240|248|252|254)\.0\.0\.0)|
(255\.(0|128|192|224|240|248|252|254)\.0\.0)|
(255\.255\.(0|128|192|224|240|248|252|254)\.0)|
(255\.255\.255\.(0|128|192|224|240|248|252|254))))(?<!\.0)$
Demo
The downside is that your JavaScript engine may not yet support negative lookbehind syntax just yet.

Regex pattern in Javascript

I want to match a string pattern which has first 4 characters, then the "|" symbol, then 4 characters, then the "|" symbol again and then a minimum of 7 characters.
For example, "test|test|test123" should be matched.
I tried RegExp("^([a-za-z0-9-|](4)[a-za-z0-9-|](5)[a-za-z0-9-|](3)+)$") for this, but it didn't match my test case.
test|test|test1234
Ramesh, does this do what you want?
^[a-zA-Z0-9-]{4}\|[a-zA-Z0-9-]{4}\|[a-zA-Z0-9-]{7,}$
You can try it at https://regex101.com/r/jilO6O/1
For example, the following will be matched:
test|test|test123
a1-0|b100|c10-200
a100|b100|c100200
But the following will not:
a10|b100|c100200
a100|b1002|c100200
a100|b100|c10020
Tips on modifying your original code.
You have "a-za-z" where you probably intended "a-zA-Z", to allow either upper or lower case.
To specify the number of characters to be exactly 4, use "{4}". You were nearly there with your round brackets, but they need to be curly, to specify a count.
To specify a range of number of characters, use "{lowerLimit,upperLimit}". Leaving the upper limit blank allows unlimited repeats.
We need to escape the "|" character because it has the special meaning of "alternate", in regular expressions, i.e. "a|b" matches either "a" or "b". By writing it as "\|" the regex interpreter knows we want to match the "|" character itself.

javascript regex : possible to have a range in the quantifier? [duplicate]

I've written a regular expression that matches any number of letters with any number of single spaces between the letters. I would like that regular expression to also enforce a minimum and maximum number of characters, but I'm not sure how to do that (or if it's possible).
My regular expression is:
[A-Za-z](\s?[A-Za-z])+
I realized it was only matching two sets of letters surrounding a single space, so I modified it slightly to fix that. The original question is still the same though.
Is there a way to enforce a minimum of three characters and a maximum of 30?
Yes
Just like + means one or more you can use {3,30} to match between 3 and 30
For example [a-z]{3,30} matches between 3 and 30 lowercase alphabet letters
From the documentation of the Pattern class
X{n,m} X, at least n but not more than m times
In your case, matching 3-30 letters followed by spaces could be accomplished with:
([a-zA-Z]\s){3,30}
If you require trailing whitespace, if you don't you can use: (2-29 times letter+space, then letter)
([a-zA-Z]\s){2,29}[a-zA-Z]
If you'd like whitespaces to count as characters you need to divide that number by 2 to get
([a-zA-Z]\s){1,14}[a-zA-Z]
You can add \s? to that last one if the trailing whitespace is optional. These were all tested on RegexPlanet
If you'd like the entire string altogether to be between 3 and 30 characters you can use lookaheads adding (?=^.{3,30}$) at the beginning of the RegExp and removing the other size limitations
All that said, in all honestly I'd probably just test the String's .length property. It's more readable.
This is what you are looking for
^[a-zA-Z](\s?[a-zA-Z]){2,29}$
^ is the start of string
$ is the end of string
(\s?[a-zA-Z]){2,29} would match (\s?[a-zA-Z]) 2 to 29 times..
Actually Benjamin's answer will lead to the complete solution to the OP's question.
Using lookaheads it is possible to restrict the total number of characters AND restrict the match to a set combination of letters and (optional) single spaces.
The regex that solves the entire problem would become
(?=^.{3,30}$)^([A-Za-z][\s]?)+$
This will match AAA, A A and also fail to match AA A since there are two consecutive spaces.
I tested this at http://regexpal.com/ and it does the trick.
You should use
[a-zA-Z ]{20}
[For allowed characters]{for limiting of the number of characters}

Regular expression to validate textbox length

I want a regular expression to validate an ASP textbox field with the minimum length of 11 characters and in the middle of the string should be a "-" sign. The sample string is: "0000-011111". I want to validate the textbox to make sure user enters a minimum of 10 numbers with "-" sign after 4 digits using regular expressions. Please help me.
Thank you.
Use
\d{4}-\d{6}
\d represents a digit, - is a literal dash and the number in the curly brackets force the preceeding token to be present the given number of times.
^\d{4}-\d{6,}$
You should use also ^ at the beginning and $ at the end to ensure that there is nothing before and after your string that you don't want to have. Also important is the {6,} so it will match at least 6 digits, without , it will match exactly 6 digits. If you want set a maximum of digits you can specify after the ,, e.g. {6,20}.

Categories