I need to match a pattern for validation . I want to match a decimal number where numeric part can have upto 14 digits including + or - without 0 and after decimal has upto 4 digits. Valid patterns are :
+1.23
9857.6543
-745290.0
Invalid patterns are:
0
0.00
1.23456
I have tried ^[0-9]{0,14}\.[0-9]{0,4}$.
I am not getting how to match for +,- and 0 condition
Short answer: ^[+-]?[1-9][0-9]{0,13}\.[0-9]{1,4}$
^ - start of string
[+-]? optionally, one between + and -
[1-9][0-9]{0,13} - a 14 digit number that doesn't start with 0
\. - decimal separator, has to be escaped or it will mean "any one character"
[0-9]{1,4} - up to 4 decimal digits
$ - end of string
This might work ^(\+|-)?(([1-9]|0(?=0*[1-9]))[0-9]{0,13}(\.[0-9]{1,4})?|0{1,14}\.(?=0*[1-9])[0-9]{1,4})$
^(\+|-)? - starts with +/-
(
([1-9]|0(?=0*[1-9]))[0-9]{0,13}(\.[0-9]{1,4})? - absolute value >= 1
| - or
0{1,14}\.(?=0*[1-9])[0-9]{1,4} - 0.**** with at least 1 non-zero digit
)
$ - end
const testcases = [
'+1.23',
'9857.6543',
'-745290.0',
'1.0',
'1.00',
'12',
'0.01',
'+001.01',
'0',
'0.00',
'1.23456',
'0.0',
'12.'];
const regex = /^(\+|-)?(([1-9]|0(?=0*[1-9]))[0-9]{0,13}(\.[0-9]{1,4})?|0{1,14}\.(?=0*[1-9])[0-9]{1,4})$/;
testcases.forEach(n => console.log(`${n}\t - ${regex.test(n)}`));
The pattern:
^[+-]?[^\D0]\d{0,13}\.\d{1,4}(?!\d)
matches the first 3 but not the second 3. [^\D0] is, if I'm not mistaken, strictly the same as [123456789], but slightly more compact.
You can assert that the string does not start with 1 or more zeroes, followed by an optional dot and optional zeroes.
Then match 1-14 digits and optionally a dot and 1-4 digits, which would also allow for 00001 for example
^(?!0+\.?0*$)[+-]?\d{1,14}(?:\.\d{1,4})?$
The pattern matches:
^ Start of string
(?!0+\.?0*$) Negative lookahead, assert not 1+ zeroes, optional dot and optional zeroes
[+-]? Optionally match + or -
\d{1,14} Match 1-14 digits
(?:\.\d{1,4})? Optionally match . and 1-4 digits
$ End of string
Regex demo
How about this?
^(?!0\.?0*$)[\+\-]?[0-9]{1,14}\.?[0-9]{0,4}$
Note: this does not match decimals without a leading 0 like .123 and does not match numbers with spaces in between the + or -. Though, these could be added to the pattern.
Requiring a non-zero digit before a . eliminates fractions of whole numbers, like 0.123.
Also, if you don't want integers and only want decimals, you will need to modify to make the . required:
^(?!0\.?0*$)[\+\-]?[0-9]{1,14}\.[0-9]{0,4}$
Related
Minimum 3 characters, all small case, can use maximum of 2 numbers, no special characters allowed.
I tried using ^[a-zA-Z0-9]*$ but I'm unable to limit the numbers used
Can someone help me.
You could use a negative lookahead assertion to rule out more than 3 digits:
/^(?!(?:.*\d+){3,})[a-z0-9]{3,}$/
Here is an explanation of the pattern:
^ from the start of the string
(?!(?:.*\d+){3,}) assert that 3 or more digits do NOT occur
[a-z0-9]{3,} then match 3 or more lowercase letters or digits
$ end of the string
Here is a working demo.
You could use check if there are at least 3 allowed characters, and then match 0, 1 or 2 digits.
^(?=[A-Za-z\d]{3})[A-Za-z]*(?:\d[A-Za-z]*){0,2}$
Explanation
^ Start of string
(?=[A-Za-z\d]{3}) Positive lookahead, assert 3 allowed chars
[A-Za-z]* Match optional chars A-Za-z
(?:\d[A-Za-z]*){0,2} Repeat 0-2 times matching a single digit and optional chars A-Za-z
$ End of string
See the matches on regex101.
export const validUKPhone = /^(\+)?(44)?(\s*\d){9,11}$/;
I currently have the following RegEx for a telephone number, that is trying to validate the length of a number.
Now the fun bit of this, is that there are 3 optional characters at the start of the pattern. +44 (potentially).
My question is how to write my regex to take account for this group, and only count the length of the 'main body' of the number. If the +44 exists, the length pattern would be {12,14} otherwise {9,11}
e.g. The following test fails.
expect(regex.test('+440798444')).toBeFalsy();
expect(regex.test('+440798444457')).toBeTruthy();
(10 characters currently because of the +44 but returns true)
You can use
/^(?:\+?44|(?!44))(?:\s*\d){9,11}$/
See the regex demo. Details:
^ - start of string
(?:\+?44|(?!44)) - a non-capturing group matching:
\+?44 - an optional + and then 44
| - or
(?!44) - (a negative lookahead that matches) a location that is not immediately followed with 44 (add \s* if you do not want to match the number even if there are whitespaces before/inside 44)
(?:\s*\d){9,11} - nine to eleven occurrences of zero or more whitespaces and then a digit
$ - end of string.
I would like to capture a string that meets the criteria:
may be empty
if it is not empty it must have up to three digits (-> \d{1,3})
may be optionally followed by a uppercase letter ([A-Z]?)
may be optionally followed by a forward slash (i.e. /) (-> \/?); if it is followed by a forward slash it must have from one to three digits
(-> \d{1,3})
Here's a valid input:
35
35A
35A/44
Here's invalid input:
34/ (note the lack of a digit after '/')
I've come up with the following ^\d{0,3}[A-Z]{0,1}/?[1,3]?$ that satisfies conditions 1-3. How do I deal with 4 condition? My Regex fails at two occassions:
fails to match when there is a digit and a forward slash and a digit e.g .77A/7
matches but it shouldn't when there isa digit and a forward slash, e.g. 77/
You may use
/^(?:\d{1,3}[A-Z]?(?:\/\d{1,3})?)?$/
See the regex demo
Details
^ - start of string
(?:\d{1,3}[A-Z]?(?:\/\d{1,3})?)? - an optional non-capturing group:
\d{1,3} - one to three digits
[A-Z]? - an optional uppercase ASCII letter
(?:\/\d{1,3})? - an optional non-capturing group:
\/ - a / char
\d{1,3} - 1 to 3 digits
$ - end of string.
Visual graph (generated here):
This should work. You were matching an optional slash and then an optional digit from 1 to 3; this matches an optional combination of a slash and 1-3 of any digits. Also, your original regex could match 0 digits at the beginning; I believe that this was in error, so I fixed that.
var regex = /^(\d{1,3}[A-Z]{0,1}(\/\d{1,3})?)?$/g;
console.log("77A/7 - "+!!("77A/7").match(regex));
console.log("77/ - "+!!("77/").match(regex));
console.log("35 - "+!!("35").match(regex));
console.log("35A - "+!!("35A").match(regex));
console.log("35A/44 - "+!!("35A/44").match(regex));
console.log("35/44 - "+!!("35/44").match(regex));
console.log("34/ - "+!!("34/").match(regex));
console.log("A/3 - "+!!("A/3").match(regex));
console.log("[No string] - "+!!("").match(regex));
I need a regex validation for mixed length, a total length of 6 characters in that 4-6 characters in caps/numbers and 0-2 spaces.
I tried like
^[A-Z0-9]{4,6}+[\s]{0,2}$
but it results in a max length of 8 characters, but I need a max of 6 characters.
If the alphanumeric chars should only appear at the start of the string and the whitespaces can appear at the end (i.e. the order of the alphanumerics and whitespaces matters), you may use
/^(?=.{6}$)[A-Z0-9]{4,6}\s*$/
See the regex demo
Details
^ - start of string
(?=.{6}$) - the string length is restricted to exactly 6 non-line break chars
[A-Z0-9]{4,6} - 4, 5 or 6 uppercase ASCII letters or digits
\s* - 0+ whitespaces (but actually, only 0, 1 or 2 will be possible to add as the total length is already validated with the lookahead)
$ - end of string.
If you want to match the alphanumeric and whitespaces anywhere inside the string, you need a lookaround based regex like
^(?=(?:[^A-Z0-9]*[A-Z0-9]){4,6}[^A-Z0-9]*$)(?=(?:\S*\s){0,2}\S*$)[A-Z0-9\s]{6}$
See the regex demo
Details
^ - start of string
(?=(?:[^A-Z0-9]*[A-Z0-9]){4,6}[^A-Z0-9]*$) - a positive lookahead that requires the presence of 4 to 6 letters or digits anywhere inside the string
(?=(?:\S*\s){0,2}\S*$) - a positive lookahead that requires the presence of 0 to 2 whitespaces anywhere inside the string
[A-Z0-9\s]{6} - 6 ASCII uppercase letters, digits or whitespaces
$ - end of string.
To shorten the pattern, the second lookahead can be written as (?!(?:\S*\s){3}), it will fail the match if there are 3 whitespace chars anywhere inside the string. See the regex demo.
You can use | characters to accommodate several cases into one.
const regex = /(^[A-Z0-9]{4}\s{2}$)|(^[A-Z0-9]{5}\s$)|(^[A-Z0-9]{6}$)/g;
alert(regex.test(prompt('Enter input, including space(s)')));
If you want to match zero, one or two spaces at the end, you could use an alternation for those 3 cases.
^(?:[A-Z0-9]{4}[ ]{2}|[A-Z0-9]{5}[ ]|[A-Z0-9]{6})$
Regex demo
Explanation
^ Assert the start of the string
(?: Non capturing group
[A-Z0-9]{4}[ ]{2} Match uppercase or digit 4 times followed by 2 spaces
| Or
[A-Z0-9]{5} Match uppercase or digit 5 times followed by 1 space
| Or
[A-Z0-9]{6} Match uppercase or digit 6 times
) Close non capturing group
$ Assert the end of the string
I would like a reg ex expression that is in phone number format (XXX)XXX-XXXX
..that can be no more than 10 digits, and does not accept 0 or 1 as the first number.
Right now I have -
/^(?!\(?[0-9]11\)?|\(?1[0-9][0-9]\)?|\(?0[0-9][0-9]?)(\(?\d{3}\)?\s?)(\(?\d{3})(\s?-?\s?)(\d{4})$/;
But it does not work.
Any help with this would be greatly appreciated.
You can use expression:
^\([2-9][0-9]{2}\)[0-9]{3}-[0-9]{4}$
^ Assert position beginning of line.
\( An opening bracket (.
[2-9] First digit between 2 and 9.
[0-9]{2} Two digits.
\) A closing bracket ).
[0-9]{3} Three digits.
- A hyphen character.
[0-9]{4} Four digits.
$ Assert position end of line.
You can test it here.
We'll match 3 numbers not starting with 0 or 1 surrounded by parenthesis with \([2-9]\d{2}\) then 3 numbers alone with \d{3} then 4 numbers following a dash with -\d{4}.
Your full expression is /\([2-9]\d{2}\)\d{3}-\d{4}/
const regex = /\([2-9]\d{2}\)\d{3}-\d{4}/;
const valid = '(211)222-3333';
const invalid = '(111)222-3333';
console.log(regex.test(valid));
console.log(regex.test(invalid));