Regex for time input [duplicate] - javascript

This question already has answers here:
How can I make part of regex optional?
(2 answers)
Closed 2 years ago.
I can have the following kinds of value I receive from the input:
1w 1d 1h 1m
or
1н 1д 1ч 1м
Where the week (w) and day (d) options are optional and the rest is obligatory. I came up with the following regex:
/^(([0-9][w,н])\s([0-9][d,д]))\s?([0-24][h,ч])\s([0-60][m,м])$/
But it's not working.
I think I messed up the optional part and I'm not quite sure about the capturing groups as well, so any help here would be greatly appreciated.

This works: (?:\d[wн]\s)?(?:\d[dд]\s)?(:?\d{1,2}[hч])\s(:?\d{1,2}[mм])
See: https://regex101.com/r/X4ObPD/2

Try:
/^(?:\d+[wн]\s+\d+[dд]\s+)?\d+[чh]\s+\d+[мm]$/

You are using the regex class operator [..] wrong. I see your intention to set the allowed range, but this is not how it works. You can only set the allowed character set here. So [0-9] simply means, all characters 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 are allowed. How many of them is specified afterwards, e.g. [0-9]+ means one or more. In your case I'd say "one or two" is what you want for minutes and hours like [0-9]{1,2}.
The following regex works as expected:
^(([0-9]+)[wн]\s([0-9]+)[dд]\s)?(([0-9]{1,2})[hч]\s)(([0-9]{1,2})[mм])$
Please note that you have to do the value validation (so hours and minutes are in the right range) afterwards programmatically.

Related

Javascript Regex plus operator usage [duplicate]

This question already has answers here:
How do I match an entire string with a regex?
(8 answers)
What is the meaning of + in a regex?
(5 answers)
Closed 6 months ago.
So I've read a lot of posts and documentation on the + operator for JS regex, but I'm still unsure of what it wouldo do.
For example, one of the websites I looked after gave the following example:
const nums = /\d+/
console.log(nums.test('456'))
Says it would look for infinite possibilites for d. But then...
const nums = /\d/
console.log(nums.test('456'))
... has the same result.
So I tried using numbers... for instance:
const nums2 = /45/
const nums 3 = /45+/
console.log(....
But testing both regex would STILL give the same result. If I put any numbers before or after 45 with or without the + it will still give me a "true" if 45 is togheter.
So can someone explain what the + symbol in regex means and it's usage in a way I can understand?
There's no difference in the cases you tried -- if there's one digit, then there's also one or more digit, so they both match.
But if you use it together with other patterns you can have a difference.
console.log(/A\dB/.test("A123B"));
console.log(/A\d+B/.test("A123B"));
The first one is false because it only matches a single digit between A and B; the second is true because it matches any number of digits.
The difference can also be useful if you use .match() instead of .test(). This returns the part of the string that matched the regexp, and \d+ will return the entire number, while \d will just return the first digit.

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

Adding a length to a regex expression [duplicate]

This question already has an answer here:
Restricting character length in a regular expression
(1 answer)
Closed 3 years ago.
Im trying to add a length requirement to the below code.
Code: ^[a-zA-z0-9\!\$\%\&]+(?: [a-zA-z0-9\!\$\%\&]+)*$
I want a sentence with spaces to only have around 1 to 10 characters. I want to count spaces as well. The code provided doesn't allow leading or trailing space but space between.
A sentence limited to 10 chars is obscenely small but you can use:
^(?=^.{1,10}$)[a-zA-z0-9\!\$\%\&]+(?: [a-zA-z0-9\!\$\%\&]+)*$
(?=^.{1,10}$) = ensure that between 1 and 10 chars exist between the start and end of the string.
https://regex101.com/r/zsa1N9/1
I see your new comment about Javascript. You would be better off adding a check for .length in addition to your regex.

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

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

Validate dateFormat in dd/mm/yyyy using regex [duplicate]

This question already has answers here:
Javascript date regex DD/MM/YYYY
(13 answers)
Closed 7 years ago.
I want to validate the format of the date value entered by a user using regex with javascript.
My regex doesn't allow the '/' character , /[^0-9\.]/g,''
But I want to let '/' pass the regex test too. What modification do I need to make here?
Modified from this answer you can be pretty exact with this. This works for the years 1000-9999, is Proleptic Gregorian and assumes that we won't change how leap-years work until the year 9999 ;)
^(?:(?:(?:0[1-9]|1\d|2[0-8])/(?:0[1-9]|1[0-2])|(?:29|30)/(?:0[13-9]|1[0-2])|31/(?:0[13578]|1[02]))/[1-9]\d{3}|29/02/(?:[1-9]\d(?:0[48]|[2468][048]|[13579][26])|(?:[2468][048]|[13579][26])00))$
Debuggex Demo
"20/11/1992".match(/^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/)
The above snippet should do, but there are too many validations to be performed on dates, so I wouldn't recommend regex.
Instead, I'd say do it like most websites do and place 3 combo boxes (dd/mm/yyyy), and allow the user to select a date, then you validate that date using the Date() constructor (if the values haven't changed, the date is correct).
note: the answer is based upon the assumption that you don't want to use any of the existing libraries (or the native validation provided by browser when using input[type="date"])
You can use this regex:
/(^(((0[1-9]|[12][0-8])[\/](0[1-9]|1[012]))|((29|30|31)[\/](0[13578]|1[02]))|((29|30)[\/](0[4,6,9]|11)))[\/](19|[2-9][0-9])\d\d$)|(^29[\/]02[\/](19|[2-9][0-9])(00|04|08|12|16|20|24|28|32|36|40|44|48|52|56|60|64|68|72|76|80|84|88|92|96)$)/
This validates the date with format dd/mm/yyyy and also checks for leap years.
It depends on how strict you need to be? I thing that simple:
/[0-3]\d\W[01]\d\W(?>19|20)\d{2}/g
should be sufficient.
day: [0-3]\d 2 digits, first 0-3, second any number \d
month: [01]\d 2 digits, first 0 or 1, second any number
year: (?>19|20)\d{2} 4 digits, starts with 19 or 20 (for 19th and 20th century) and next any digit two times {2}
Also note, I used \W to match single non-word character as workaround to match /. Are you sure that you cannot use escaped slash \/ instead?

Categories