I have a regex that validates UAE numbers like: 00971585045336
here is the regex:
/00971(?:50|51|52|53|54|55|56|57|58|59|2|3|4|6|7|9)\d{7}$/
I have a requirement to add support for toll free numbers like:
0097180038249953 or 0097180022988
I am not good with regex so I need help to make it possible.
Thanks in advance.
You can use the following regex, assuming the tool free number format is 00971800, followed by 5 or 8 digits. Your original regex is simplified with character classes. The test shows 4 valid numbers, followed by invalid numbers:
const regex = /^00971((5\d|[234679])\d{7}|800(\d{5}|\d{8}))$/;
[
'00971581234567',
'0097171234567',
'0097180012345',
'0097180012345678',
'0097158123456',
'009715812345678',
'009717123456',
'00971712345678',
'00971800123456',
'009718001234567',
].forEach((str) => {
let valid = regex.test(str);
console.log(str + ' ==> ' + valid);
});
Output:
00971581234567 ==> true
0097171234567 ==> true
0097180012345 ==> true
0097180012345678 ==> true
0097158123456 ==> false
009715812345678 ==> false
009717123456 ==> false
00971712345678 ==> false
00971800123456 ==> false
009718001234567 ==> false
Explanation:
^ - start of string
00971 - expect literal text
( - start group, used for logical OR
( - start group, used for logical OR
5\d - expect a 5, followed by a digit
| - OR
[234679] - character class with a single char of allowed digits
) - end of group
\d{7} - 7 digits
| - OR
800 - literal text
( - start group, used for logical OR
\d{5} - 5 digits
| - OR
\d{8} - 8 digits
) - end of group
) - end of group
$ - end of string
Related
I have this regex ^[a-zA-Z0-9*]+$ for only allowing alphanumeric chars and allow Asterisk(*). But I would like allow asterisk only at the start of the string. But asterisk is not allowed at the last 4 digits of the string.
new RegExp('^[a-zA-Z0-9*]+$').test('test') ---Valid
new RegExp('^[a-zA-Z0-9*]+$').test('test1234') --Valid
new RegExp('^[a-zA-Z0-9*]+$').test('test##_')--Invalid
new RegExp('^[a-zA-Z0-9*]+$').test('****1234') --Valid
new RegExp('^[a-zA-Z0-9*]+$').test('*tes**1234') --Valid
new RegExp('^[a-zA-Z0-9*]+$').test('test****') --Should be Invalid
"How would I allow Asterisk only at the start of the string?" But if the asterisk presents in any of the last 4 positions then it should be invalid
You can use this regex to allow only alphanumeric chars and asterisk, but no asterisk at the last 4 char positions:
const regex = /^(?:[a-z\d*]*[a-z\d]{4}|[a-z\d]{1,3})$/i;
[
'1',
'12',
'test',
'test1234',
'****1234',
'*tes**1234',
'*1*2345',
'test##_',
'test****',
'test***5',
'test**4*',
'*3**'
].forEach(str => {
let result = regex.test(str);
console.log(str, '==>', result);
});
Output:
1 ==> true
12 ==> true
test ==> true
test1234 ==> true
****1234 ==> true
*tes**1234 ==> true
*1*2345 ==> true
test##_ ==> false
test**** ==> false
test***5 ==> false
test**4* ==> false
*3** ==> false
Explanation of regex:
^ -- anchor at start of string
(?: -- start non-capture group (for logical OR)
[a-z\d*]*[a-z\d]{4} -- allow alphanumeric chars and asterisk, followed by 4 alphanumeric chars
| -- logical OR
[a-z\d]{1,3} -- allow 1 to 3 alphanumeric chars
) -- close group
$ -- anchor at end of string
Not that it is easier to read and more efficient to use /.../ instead of new RegExp("..."). You need the regex constructor only if you have variable input.
I have a problem with my price regex which I'm trying to change. I want it to allow numbers like:
11111,64
2 122,00
123,12
123 345,23
For now I have something like this, but it won't accept numbers without spaces.
'^\d{1,3}( \d{3}){0,10}[,]*[.]*([0-9]{0,2'})?$'
I tried changing ( \d{3}) to (\s{0,1}\d{3}) but it still doesn't work :(
All problems are easier if you break them into pieces.
First we have to match the non decimal
1
100
1 000
10 000 000
The first grouping is 1 to 3 digits or \d{1,3}
We still need to account for the following groups which may or may not be there. That in regex is a * or 0 or many \d{1,3}(\s\d{3})* in that second part we put a space in front of the set to it now looks for spaces between groups of 3.
To complete this set we add in a \d+ for a flat block of numbers
Last we have to match the decimal, optionally ?. ^(\d{1,3}(\s\d{3})*|\d+)(,\d+)?$
/^(\d{1,3}(\s\d{3})*|\d+)(,\d+)?$/.test(str)
Test it some more here: https://regex101.com/r/NKAVLk/1
[
'1',
'123',
'11111,64',
'2 122,00',
'123,12',
'123 345,23',
'2 12,00', // no match, pair of 2 digits
'2 1234,00', // no match, pair of 4 digits
'1,123' // no match, > 2 digits after comma
].forEach(str => {
let t = /^(:?\d{1,3}( \d{3})*|\d+)(,\d{0,2})?$/.test(str);
console.log(str + ' => ' + t);
});
Output:
1 => true
123 => true
11111,64 => true
2 122,00 => true
123,12 => true
123 345,23 => true
2 12,00 => false
2 1234,00 => false
1,123 => false
Explanation of regex:
^ -- anchor at start
(:? -- non-capture group start (for logical OR)
\d{1,3} -- expect 1 to 3 digits
( \d{3})* -- expect 0+ patterns of space and 3 digits
| -- logical OR
\d+ -- expect 1+ digits
) -- non-capture group end
(,\d{0,2})? -- optional pattern of comma and 0 to 2 digits
$ -- anchor at end
I have couple specific string formatting i want to achieve for different entities:
Entity 1: 1111-abcd-1111 or 1111-abcd-111111
Entity 2: [10 any symbol or letter(all cap) or number]-[3 letters]
Entity 3: [3 letters all cap]-[3 any]-[5 number]
Not sure if Regex is best approach, because i also want to use this as validator when user starts typing the char's it will check against that Entity selected and then against it's RegEx
Here is a regex with some input strings:
const strings = [
'1111-abcd-1111', // match
'1111-abcd-111111', // match
'1111-abcd-1111111', // no match
'ABCS#!%!3!-ABC', // match
'ABCS#!%!3!-ABCD', // nomatch
'ABC-#A3-12345', // match
'ABC-#A3-1234' // no match
];
const re = /^([0-9]{4}-[a-z]{4}-[0-9]{4,6}|.{10}-[A-Za-z]{3}|[A-Z]{3}-.{3}-[0-9]{5})$/;
strings.forEach(str => {
console.log(str + ' => ' + re.test(str));
});
Result:
1111-abcd-1111 => true
1111-abcd-111111 => true
1111-abcd-1111111 => false
ABCS#!%!3!-ABC => true
ABCS#!%!3!-ABCD => false
ABC-#A3-12345 => true
ABC-#A3-1234 => false
Explanation of regex:
^ - anchor text at beginning, e.g. what follows must be at the beginning of the string
( - group start
[0-9]{4}-[a-z]{4}-[0-9]{4,6} - 4 digits, -, 4 lowercase letters, -, 4-6 digits
| - logical OR
.{10}-[A-Za-z]{3} - any 10 chars, -, 3 letters
| - logical OR
[A-Z]{3}-.{3}-[0-9]{5} - 3 uppercase letters, -, any 3 chars, -, 5 digits
) - group end
$ - anchor at end of string
Your definition is not clear; you can tweak the regex as needed.
Need regex for following combination.
Only Numbers
Max 2 digit after decimal
following types of range available
5
8.95
>2.5
<5.65
>=4.24
<=7.2
1.2-3.2
i tried below regex which accept number, two decimal no and should not end with <>=. special characters.
/^(?!.*<>=.$)[0-9]+(\.[0-9]{1,2})?$/gm
Need regex for range values
You could match either a single occurrence of a number preceded by a combination of <>= or match 2 numbers with a hyphen in between.
^(?:(?:[<>]=?)?\d+(?:\.\d{1,2})?|\d+(?:\.\d{1,2})?-\d+(?:\.\d{1,2})?)$
^ Start of string
(?: Non capture group for the alternation
(?:[<>]=?)? Optionally match < > <= >=
\d+(?:\.\d{1,2})? Match 1+ digits with optional decimal part of 1-2 digits
| Or
\d+(?:\.\d{1,2})?-\d+(?:\.\d{1,2})? Match a digits format with a hyphen in between
) Close group
$ End of string
See a regex demo
const pattern = /^(?:(?:[<>]=?)?\d+(?:\.\d{1,2})?|\d+(?:\.\d{1,2})?-\d+(?:\.\d{1,2})?)$/;
[
"5",
"8.95",
">2.5",
"<5.65",
">=4.24",
"<=7.2",
"1.2-3.2",
"1-1.3",
"1-4",
">2",
">=4",
"2.5>",
"1.123"
].forEach(s => console.log(`${s} ==> ${pattern.test(s)}`));
This regex does what you ask for. The test has first some matches, followed by non-matches:
const regex = /^(([><]=?)?[0-9]+(\.[0-9]{1,2})?|[0-9]+(\.[0-9]{1,2})?-[0-9]+(\.[0-9]{1,2})?)$/;
[
'1',
'12',
'12.2',
'12.34',
'>1.2',
'>=1.2',
'<1.2',
'<=1.2',
'1.2-3.4',
'1.22-3.44',
'x1',
'1.234',
'1.222-3.444'
].forEach((str) => {
let result = regex.test(str);
console.log(str + ' ==> ' + result)
})
Output:
1 ==> true
12 ==> true
12.2 ==> true
12.34 ==> true
>1.2 ==> true
>=1.2 ==> true
<1.2 ==> true
<=1.2 ==> true
1.2-3.4 ==> true
1.22-3.44 ==> true
x1 ==> false
1.234 ==> false
1.222-3.444 ==> false
can you help me with regex for :
full name -
can be in english or hebrew [\u0590-\u05FF]-this is the hebrew letter range.
need to be 2 or more words,
that every words contains at least one letter
(doesnt metter the upper or lower case)
Exmaples: Roei Grin, R G, roei grin, r G, roei gr iN,
"רועי גרין","רו ג", רועי גרי ן"
phone number-
must be 10 digits.
must start with 0
can have (not must) the "-" char, in the three or fourth place.
Exmaples: 0549129393, 058-9210348, 0266-391059
Here is a solution for both. Names and numbers have good examples, followed by bad examples:
const testNames = [
'Roei Grin',
'R G',
'roei grin',
'r G',
'roei gr iN',
'רועי גרי ן',
'רו ג',
'רועי גרין',
'Bad'
];
const testNumbers = [
'0549129393',
'058-9210348',
'0266-391059',
'1111',
'011111111',
'0999999-999',
'09999999999'
];
const nameRegex = /^[a-zA-Z\u0590-\u05FF]+( [a-zA-Z\u0590-\u05FF]+)+$/;
const numberRegex = /^0(\d{9}|\d{2}-\d{7}|\d{3}-\d{6})$/;
console.log('testNames:')
testNames.forEach(str => {
console.log('- "' + str + '" ==> ' + nameRegex.test(str));
});
console.log('testNumbers:')
testNumbers.forEach(str => {
console.log('- "' + str + '" ==> ' + numberRegex.test(str));
});
Output:
testNames:
- "Roei Grin" ==> true
- "R G" ==> true
- "roei grin" ==> true
- "r G" ==> true
- "roei gr iN" ==> true
- "רועי גרי ן" ==> true
- "רו ג" ==> true
- "רועי גרין" ==> true
- "Bad" ==> false
testNumbers:
- "0549129393" ==> true
- "058-9210348" ==> true
- "0266-391059" ==> true
- "1111" ==> false
- "011111111" ==> false
- "0999999-999" ==> false
- "09999999999" ==> false
Explanation of nameRegex:
^ ... $ - anchor at start and end of string
[a-zA-Z\u0590-\u05FF]+ - start with 1+ characters of alphabet and/or hebrew
( [a-zA-Z\u0590-\u05FF]+)+ - followed by 1+ pattern of: single space, followed by 1+ characters of alphabet and/or hebrew
Explanation of numberRegex:
^ ... $ - anchor at start and end of string
0 - start with 0
(\d{9}|\d{2}-\d{7}|\d{3}-\d{6}) - followed by either:
9 digits
or 2 digits, -, 7 digits
or 3 digits, -, 6 digits
You can use, this reg exp for your mask phone - ^[0]{1}(([\d]{2}[-]{0}[\d]{7})|([\d]{3}[-]{1}[\d]{6})|([\d]{2}[-]{1}[\d]{7}))