I am trying to match these use cases in Javascript:
a) 8 Months
b) 18 Years
Numbers could range from 1 to 20 and the strings could only be either "months" or "years"
I am trying this
[0-9]\s(Years|Months)
But it is not working.
You can use this:
([1-9]|1[0-9]|20)\s(Years|Months)
where:
[1-9] matches a digit from 1 to 9
1[0-9] matches a number from 10 to 19
20 matches just 20
Edit: As noticed in a comment, you should use
^([1-9]|1[0-9]|20)\s(Years|Months)$
if the whole string must exactly match with this text.
Another option is prepending the regex with a word boundary (\b) in order to prevent matching cases like "42 Years".
Related
This question already has answers here:
Using regular expressions to validate a numeric range
(11 answers)
Closed 2 years ago.
I am trying to write a regular expression which can match enter number string which should be in range of
-50 to 100000000
I have tried expression like
^(-?)(1000|[0-9][0-9][0-9]?)$
but it matches only -1000 to 1000 numbers.
example for to test are simple
-50
-39
9
1000
36900
2000022
Please help me to get such expression which can be match range -50 to 100M
I know we can write simple if condition but I want regex only.
Thanks in Advance.
You can use the following regex:
/^(-50|-[1-4]?\d|100000000|[1-9]?\d{1,7})$/gm
Explanation:
^ match start of string
-50 match -50
OR
- only negative numbers
[1-4]? match 1 to 4 - optional, followed by: any number
OR
100000000 match 100000000
OR
[1-9]? match 1 to 9 - optional followed by:
\d{1,7} match any number 1 to 7 times
$ match end of line
Use global and multiline options.
Regular expression serving leading zeros and positive and negative sign. Works only for integers.
^(-0*(50|[0-4]?[0-9])|\+?0*([0-9]{1,8}|100000000))$
Test case
Using with JavaScript:
let number = -50; /* <-- test number or string here */
let withinTheRange = Boolean(/^(-0*(50|[0-4]?[0-9])|\+?0*([0-9]{1,8}|100000000))$/.exec(number));
I need to validate a streetNumber. It has to be a maximum of 4 in length and first 3 should be a number and the last can be a letter( no special character allowed). The letter is not compulsory.
Example 2, 34A, 45, 101 or 121B are all possible numbers. I have this regex but it is not working for me. Can anyone help me.
^[1-9]|[A-Za-z]{1,4}
I expect 2, 34A, 45, 101 or 121B to all pass the regex check
^[1-9]\d{0,2}[A-Za-z]?$
Explanation:
[1-9] a digit between 1 and 9.
\d{0,2} zero or more (up to 2) digits.
[A-Za-z]? a letter (optional)
https://regexr.com/4br78
In case you want to allow 4 digit numbers:
^[1-9]\d{0,2}[A-Za-z\d]?$
Simply use ^[1-9]\d{0,2}[A-Z]?$
Explanation:
^ - beginning of the string
[1-9] - match 1 through 9
\d{0,2} - match zero to two digits
[A-Z]? - match zero or one letter
$ - end of the string
Demo
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
so I'm making this regular expression to verify some text boxes on a website that I'm designing for an internship.
The problem is that I'm not so keen on regular expressions, and I'm close to having a working one that matches a number between 0-24 and no more than two decimal places.
This is what I have so far. The pattern is also matching any string; such as, "a" or "az".
var pattern = "^([0-9]{0,2}?.?[0-9]{0,2}|1[0-9].?[0-9]{0,2}|2[0-4].?[0-9]{0,2})$";
To get a number between 0 and 24 (24 excluded) with optional up to two decimal places:
^(\d|1\d|2[0-3])(\.\d{1,2})?$
The decimal part:
\. - match the decimal dot
\d{1,2} - one or two digits
()? - makes it optional
The whole part:
\d - numbers 0-9
1\d - numbers 10-19
2[0-3] - numbers 20-23
(x|y|z) - one of x, y or z
As for the "why is my version matching things like "a" and "az" part" - it's a little complex, but it basically boils down to you using dots (like .?). In regex, a dot means "any one character". To make it match a literal dot, you need to escape it with a slash just like I did.
Minor remark: If you want optional leading zero for single digit numbers, replace 1\d with [01]\d. If you want mandatory leading zero for single digit numbers, replace \d|1\d with [01]\d. If you don't want leading zeroes, leave it as it is.
Assuming you do not want 05 or 5.50
^((?:[0-9]|1[0-9]|2[0-3])(?:\.(?:[1-9]|[0-9][1-9]))?)$
You can try it here
The following is a quick attempt to match a floating point number from 0 to 24.99 with up to two non-zero digits
^(([0-9])|([01][0-9])|(2[0-4]))(\.[0-9]{1,2})?$
I think it might be easier to use math to do this though...
You can see the explanation of the entire regex as well as test it out here. I have also added a few test cases.
^(\d|[01]\d|2[0-3])(\.\d{1,2})?$
Test cases:
Valid:
22
1.29
2.99
9.99
13.24
17.38
20.01
02.15
15.35
23.56
1.1
Invalid:
24.29
235.215
21.256
To get a integer number between 1 and 23: ^([1-9]|1[0-9]|2[0-3])$
Goal:
I am trying to create a more efficient regular expression to flexibly capture SEPA bank numbers for form validation.
Background:
SEPA IBAN numbers have this pattern: NL88ABNA1234567890
My question is concerning the last 9 or 10 numbers at the end e.g. 0123456789.
I want to allow any combination of space characters and dashes as the numbers are sanitised before submission, e.g.:-
0123456789
01-23-45-67-89
01 23 45 67 89
01 - 23 - 45 - 67 - 89
012 345 67 89
etc etc
I can achieve this with the following:-
(\d(?:\s[\s-]\s|[\s-])?\d(?:\s[\s-]\s|[\s-])?\d(?:\s[\s-]\s|[\s-])?\d(?:\s[\s-]\s|[\s-])?\d(?:\s[\s-]\s|[\s-])?\d(?:\s[\s-]\s|[\s-])?\d(?:\s[\s-]\s|[\s-])?\d(?:\s[\s-]\s|[\s-])?\d(?:\s[\s-]\s|[\s-])?\d?)
But it's not very elegant
I tried combining capturing group with a quantifier but it only returns the last digit:-
(\d(?:\s?[\s-]\s?)?){9,10}
How can I achieve this more efficiently & check the whole number not just last digit?
You can use this regex with optional spaces and hyphen following your digits:
/((?:\d[\s-]*){9,10})/
[\s-]* will allow 0 or more spaces and hyphens after each digit.
RegEx Demo