Need regex for range values - javascript

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

Related

JavaScript Alphanumeric Regex and allow asterisk at the start of the string but do not allow asterisk at the last 4 digits of the string

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.

How to check character and special character in regex javascript (allow input , and .) [duplicate]

This question already has answers here:
Regular expression to match numbers with or without commas and decimals in text
(11 answers)
Closed 3 months ago.
var regex = /^(?=.*[_$#])(?=.*[^_$#.])[\w$#]$/;
var text = "12a/d";
console.log(regex.test(text))
it's not working.
I want it's allow input only number and float ex: '12.2,2' , '12', '12.2', '2,2'
Thank everyone
I am not clear about your number format examples. Is 12.2,2 two numbers separated by comma? Is 2,2 the German locale representation of a floating number?
Here is a number validation for the English locale with valid and invalid numbers as input:
const input = [
'0',
'1',
'1.',
'1.2',
'-1.2',
'+1.2',
'.123',
'123.45',
'1,123.45',
'123,123.45',
'1,123,123.45',
'', // invalid
'1-2', // invalid
'1+', // invalid
'1x', // invalid
'1,12.9', // invalid
'1,1234.9', // invalid
];
const re = /^[+-]?((\d+|\d{1,3}(,\d{3})+)(\.\d*)?|\.\d+)$/
input.forEach(str => {
console.log( str + ' => ' + re.test(str));
});
Output:
0 => true
1 => true
1. => true
1.2 => true
-1.2 => true
+1.2 => true
.123 => true
123.45 => true
1,123.45 => true
123,123.45 => true
1,123,123.45 => true
=> false
1-2 => false
1+ => false
1x => false
1,12.9 => false
1,1234.9 => false
Explanation of regex:
^ -- anchor at beginning of string
[+-]? -- optional + or -
( -- group 1 start
( -- group 2 start
\d+ -- 1+ digits
| -- logical OR in group 2
\d{1,3}(,\d{3})+ -- 1 to 3 digits, followed by 1+ sets of comma and 3 digits
) -- group 2 end
(\.\d*)? -- optional dot and 0+ digits
| -- logical OR in group 1
\.\d+ -- a dot an 1+ digits
) -- group 1 end
$ -- anchor at end of string

Regex for decimal prices with or without spaces

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

Struggling with RegEx validation and formating for specfici ID format

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.

Regex to support toll free numbers along with UAE numbers

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

Categories

Resources