I am trying to validate minutes:seconds input where minutes can be 07 or 7.
I can get 07:35 validated using below but not 7:35. When I process the input I can append a zero if values is less than 9 but want to be able to let users type 7:35 as well.
^([0-5]\d:[0-5]\d$)
You may make the first munite digit optional:
^[0-5]?\d:[0-5]\d$
^
See the regex demo
Details
^ - start of string
[0-5]? - an optional (1 or 0 repetitions) of a 0 to 5 digit
\d - any 1 digit
: - a : char
[0-5] - a digit from 0 to 5
\d - any 1 digit
$ - end of string.
Related
This question already has answers here:
Regex decimal values between 0 and 1 up to 4 decimal places
(2 answers)
Closed 2 years ago.
I basically want a regex for alpha in RGBA which is always a positive number between 0 & 1. However, I want it to only be upto 2 digits like 0.53 & not more than that like 0.536.
Allowed
Anything between 0 to 1 but only upto 2 decimal places
0
0.0
0.00
0.1
0.12
0.34
1
1.0
1.00
Not Allowed
Anything outside of 0 to 1 & if its between 0 to 1 then it should be less than or equal to 2 decimal places only & even signs not allowed
0.123
90
3
-1
+1
I noticed other similar questions but they allow signs or they allow more than 2 decimal places.
Currently, I have a regex like /^(0+\.?|0*\.\d+|0*1(\.0*)?)$/ which allows for more than 2 decimal places. How do I solve it?
You may use:
^(?:0(?:\.[0-9]{1,2})?|1(?:\.00?)?)$
RegEx Demo
RegEx Details:
^: Start
(?:: Start a non-capture group
0: Match 0
(?:\.[0-9]{1,2})?: Match optional dot followed by 1 or 2 digits
|: OR
1: Match 1
(?:\.00?)?: Match optional 1 or 2 zeroes after dot
): End non-capture group
$: End
Alternatively, try:
^(?!1..?[1-9])[01](?:\.\d\d?)?$
See the online demo
^ - Start string anchor.
(?! - Open negative lookahead:
1..? - A literal "1" followed by any character other than newline and an optional one.
[1-9]- Match a digit ranging from 1-9.
) - Close negative lookahead.
[01] - Match a zero or one.
(?: - Open non-capture group:
\.\d\d? - Match a literal dot, a single digit and an optional one.
)? - Close non-capturing group and make it optional.
$ - End string anchor.
Trying to create a regex that could match numbers from large document.
Find at least 10 continuous digits (which can go to maximum 15 digits) that could be separated by one or multiple
-
_
\s
(
)
[
]
Tried-
/(?:((\d([ \-_\s]+?)){5,8}))/
Eg:
1-2-3-4-5-6-7-8-9-0-12-34
1 2 3 4 5 6 7 8 9 0
123-456-789-0
123---456---789---987
12 34 56 78 90
12_ -34_-56--78__90
You may use
/\d(?:[-_\][()\s]*\d){9,14}/g
See the regex demo
Details
\d - a digit
(?:[-_\][()\s]*\d){9,14} - 9 to 14 repetitions of
[-_\][()\s]* - 0 or more repetitions of -, _, ], [, (, ) or whitespace
\d - a digit.
Note you do not need to escape [ inside a character class, it is parsed as a literal [ in a JS regex. However, ] must be escaped there, otherwise, it will close the character class prematurely.
I am really struggling with creating a Reg Ex for Julian Day that does not allow leading zeros.
I am using it for Input Validation using JavaScript Reg Ex. It needs to match 1 through 366.
Example Matches:
1
99
366
159
Example Match Failures:
01
001
099
367
999
0
I tried this on regex101:
^[1-9]|[1-9][0-9]|[1-3][0-5][0-9]|36[0-6]$
But for I am not getting the optional parts down right. So when I put in 266, I get a match on 2 and 66. (This issue is translating to my input validation control.)
I thought about trying to use + for one or more, but I need to not allow leading zeros, so that does not work.
I have read the guidance on asking a RegEx question and tried to follow it, but if I missed something, please let me know and I will update my question.
The main issues are two: 1) the alternatives should have been grouped so that ^ and $ anchors could be applied to all of them, 2) the [1-3][0-5][0-9] part did not match 160 to 199 and 260 to 299, this part should have been split into two separate branches, [12][0-9]{2}|3[0-5][0-9].
You may use
^(?:[1-9]|[1-9][0-9]|[12][0-9]{2}|3[0-5][0-9]|36[0-6])$
See the regex demo.
Details
^ - start of string
(?: - group of alternatives:
[1-9] - 1 to 9
| - or
[1-9][0-9] - 10 to 99
| - or
[12][0-9]{2} - 100 to 299
| - or
3[0-5][0-9] - 300 to 359
| - or
36[0-6] - 360 to 366
) - end of the alternation group
$ - and the end of the string.
I use this regex for my field:
/^([1-9]([0-9]){0,3}?)((\.|\,)\d{1,2})?$/;
What I want to do is to allow the user to enter 0 as a beginning of the number, but in this cas, he must enter at least a second digit diffrent from 0 and the same rule is applied for the third and fourth digits.
Example:
01 -> valid
00 -> not valid
0 -> not valid
1 -> valid
In short, zero value must not be allowed. How can I do this using Regex? or would it be better if I just do it with a javascript script?
If you want to only match numbers that have 1 to 4 digits in the part before a decimal separator, and up to 2 digits after the decimal separator (I deduce it from the regex you used) and that do not start with 00 (that requirement comes from your verbal explanation), use
/^(?!00)(?!0+(?:[.,]0+)?$)\d{1,~4}(?:[.,]\d{1,2})?$/
See the regex demo.
Details
^- start of string
(?!00) - no two 0s at the start of the string
(?!0+(?:[.,]0+)?$) - a negative lookahead that fails the match if there are one or more 0s, followed with an optional sequence of . or , followed with one or more zeros up to the string end
\d{1,4} - any 1 to 4 digits
(?:[.,]\d{1,2})? - 1 or 0 occurrences of
[.,] - a . or ,
\d{1,2} - any 1 or 2 digits
$ - end of string.
JS demo:
var ss = ['1','1.1','01','01.3','023.45','0','00','0.0','0.00','0001'];
var rx = /^(?!00)(?!0+(?:[.,]0+)?$)\d{1,4}(?:[.,]\d{1,2})?$/;
for (var s of ss) {
var result = rx.test(s);
console.log(s, "=>", result);
}
Try combining JS and RegEx:
if (parseInt(value) != 0 && value.test(/\d+([,.]\d{1,2})?/)) {
//valid
}
I'm wondering if there is a way to have a regex for a phone number that takes the following forms using Javascript:
0610101010 => 10 digits that starts with 06 or 05.
+212565656566 => (+) followed by 212 then another 9 digits.
Thank You.
This should work:
/^(0(6|5)\d{8}|\+212\d{9})$/
Try something like this:
/0(5|6)[0-9]{8}|\+212[0-9]{9}/
Explained:
/ - The start of the regex
0 - Matches a zero
(5|6) - Matches a five or six
[0-9]{8} - Matches eight characters in the range zero to nine
| - Second expression starts here
\+212 - Matches a plus followed by 212
[0-9]{9} - Matches nine characters in the range zero to nine
/ - End of regex