regEx for a range of 8600000–9999999? [duplicate] - javascript

This question already has answers here:
RegExp range of number (1 to 36)
(8 answers)
Closed 6 years ago.
I'm looking for a way to validate a phone number with the length of 7, and within the rage of 8600000–9999999 (This is a region in New Zealand, witch is why google was no help as no one talks about New Zealand -- ever). Does anyone know a regEx that can do this?
I'm new to JS, but I think that a Regular Expression would be the best way to do this. (If not let me know.)
Note: The input data type of the phone number is text. (I know I can use number but I ran into bugs with it.)
EDIT -- no lines / separation, just numbers. (Also very fast answers just wow)
Thanks in advance !

This one's actually pretty easy:
/8[6-9][0-9]{5}|9[0-9]{6}/
As long as you don't need separators, anyways.
It's also pretty simple to follow:
8 matches the literal character 8.
[6-9] matches any single character that is in the ASCII range between the characters 6 and 9 (inclusive). This means that it will match any single 6, 7, 8, or 9.
Likewise, [0-9] matches any single character that is in the ASCII range betwen the characters 0 and 9. This is synonymous with "any single digit".
{5} means "match the preceding token 5 times". In this case, it's applied to [0-9], meaning it'll match 5 digits in a row.
The | (pipe) character in a regex is an alternation - it means "match either the pattern on the right or the left". This is how the regex handles the two different cases - 8600000-8999999 is handled by the pattern on the left, while 9000000-9999999 is handled by the pattern on the right.

You can use the regular expression /(?:9[0-9]|8[6-9])[0-9]{5}/.
You can click to play with it on regex101.com, and see some test cases.
Here's the breakdown (provided by regex101.com):
(?:9[0-9]|8[6-9]) Non-capturing group
1st Alternative: 9[0-9], left of |
9 matches the character 9 literally
[0-9] match a single character present in the list below
0-9 a single character in the range between 0 and 9
2nd Alternative: 8[6-9], right of |
8 matches the character 8 literally
[6-9] match a single character present in the list below
6-9 a single character in the range between 6 and 9
[0-9]{5} match a single character present in the list below
0-9 a single character in the range between 0 and 9
{5} exactly 5 times

Related

Can regex find patterns like this? [duplicate]

This question already has answers here:
Regex to detect one of several strings
(7 answers)
Closed 2 years ago.
I am learning more and more about regex and while reading about it I was wondering if regex can do something like this. Looking in a string for, for example numbers that are 3 digits long, where the second digit is 2 higher than the first digit, and the third digit is 4 higher than the second digit.
For example:
Matches:
137,
248,
359
Would regex be capable of such thing? From my limited knowdlege it does not seem to be possible.
Regex is only capable of matching string, not performing mathematcial logic on numbers, so it is not able to determine that two digits differ by certain quantity.
So answer is no.
BUT, that rule limits 3-digit numbers to a few, so you can write regex alternation to do that:
(?:137|248|359)
Explanation:
(?:...) - non-cpaturing group
| - alternation

Regular expression for UK phone number - variations for all plausible options [duplicate]

This question already has answers here:
Regular expression for GB based and only numeric phone number
(5 answers)
Closed 4 years ago.
So, after some user confusion, I need to tweak a regular expression for all phone number variations you could think of.
I currently have the following regular expression:
^[+]?[-\s.]?[(]?[0-9]{3}[)]?[-\s.]?[0-9]{3}[-\s.]?[0-9]{4,6}$
It matches fairly well, but I'd like to be able to match this to the following variations:
+44 01726 400480
+44 1726 400480
+44 01726 400 480
+44 1726 400 480
01726 400480
01726 400 480
N.B. I understand that similar questions to this have been asked previously, but I feel that adding the above variations would be perfect for the community.
You can use
^\+?(?:[- .]?\d{2})?[- .]?\d{4,5}[- .]?\d{3}[- .]?\d{3}$
https://regex101.com/r/DiJLVb/2
Note that [0-9] simplifies to \d, and that character sets with only a single character in them simplify to just that character. Also note that using a literal space is a bit more reliable than \s if you only want to match a literal space - otherwise, other whitespace characters like newlines will be matched too, which may well not be desirable.
Details:
\+? - Optional plus
(?:[- .]?\d{2})? - Optional leading two digits
[- .]?\d{4,5} - Four or five digits
[- .]?\d{3}[- .]?\d{3} - Six digits, possibly separated by a character
you can try the following https://www.regexpal.com/?fam=105302
^[+]?[-\s.]?[(]?[0-9]{2,3}?[)]?[-\s.]?([0-9]{3,4})?[-\s.]?[0-9]{3,6}[-\s.]?[0-9]{3,6}$

Regex to check pattern with numbers

I want to create a regex that does the following
<\numberMAX8>[space or not]<\symbol(-)>[space or not]<\numberMAX8> and max 10 times of all of this - I don't care about end spaces, also numbers must be between 5-8.
To explain it a bit more I'll give a few examples
ex:
5-6 7-6 8-8 6-7 ok
4-7 not ok //because of 4
7 - 6 ok
7-6-6-6 not ok because of the - in the middle
Below is what I have so far without having included the mid spaces.
^([5-8](?:-|\s)[5-8][\s]){1,10}
-> <-//didnt work.
Here you go:
^([5-8]\s*-\s*[5-8]\s*){1,10}$
So the explanation is:
The regex matches a starting number from 5-8 ^[5-8], then an arbitrary number of spaces \s*, then dash -, then arbitrary number of spaces \s*, then a number from 5 to 8 [5-8], then an arbitrary number of spaces \s*, and that pattern from 1 to 10 times {1,10}, and nothing after the pattern $.

How to create a regular expression to accept only upto 12 digits? [duplicate]

This question already has answers here:
6 digits regular expression
(7 answers)
Closed 6 years ago.
How to create a regular expression to accept only upto 12 digits?
Many Thanks
/^\d{0,12}$/
... which breaks down as ...
/ # start regex
^ # anchor to start of string
\d # 0-9
{0,12} # 0-12 times
$ # anchor end of string
/ # end regex
(?:^|[^0-9])([0-9]{1,12})(?![0-9])
I have divided problem to 3 part according to answers.
Problem want to does not start with digit
(?:^|[^0-9]) means that starts with non digit character or does not start with any character
Problem want to consume 12 digit:
[0-9] means that we want to consume just digits
{1,12} means that we want to consume up to 12 character
Problem want to does not consume these 12 digit if 13rd character is digit also.
? means that look but does not consume
![0-9] means that this character could be everything except digit.

I need REGEXP for alpha Numeric zip code, which contains minimum 3 & maximum 10 values [duplicate]

This question already has answers here:
JavaScript regex for alphanumeric string with length of 3-5 chars
(3 answers)
Closed 7 years ago.
I need REGEXP for alpha Numeric zip code, which contains minimum 3 & maximum 10 values.
Invalid inputs are: AAAAA, A1, AA, 12, 2A
Valid inputs are: 123456, 123, A1234, A12, A12A, A9A
This is the regex I'm currently using:
/(^[A-z0-9]\d{3,10})+$/
It doesn't allow to specify only digits like this 12345 but input like A123 matches correctly.
The question is not completely clear. If you mean that you can use between 3 and 10 characters, and these characters can be alphanumerical characters (digits and [A-Za-z]), you can use:
/^(?=.*\d.*)[A-Za-z0-9]{3,10}$/
regex101 demo.
The regex works as follows:
^[A-Za-z0-9]{3,10}$ says the regex consists out of 3 to 10 characters that can be digits and/or A-Z/a-z.
The lookahead (?=.*\d.*) enfoces that the string contains at least one digit somewhere in the string.

Categories