Reputation: 1118
I am trying to validate a comma separated list of numbers 1-384 unique (not repeating).
i.e.
I have tried the following RegEx pattern, but is not sufficient:
/^(?!.*(\b(?:[1-9]|[1-2]\d|3[0-1])\b).*\b\1\b)(?:[1-9]|[1-2]\d|3[0-1])(?:,(?:[1-9]|[1-2]\d|3[0-1]))*$/
Upvotes: 2
Views: 233
Reputation: 177684
A filter and set might be easier on the eyes
is "1-385" also false?
const isValid = str => {
str = str.replace(/\s+/g, "")
const rng = str.split(",").filter(item => {
if (item === null || item === "") return false
if (isNaN(item)) {
const [from, to] = item.split('-')
return +from > 0 && +from < 385 &&
+to > 0 && +to < 385
}
item = +item
return item > 0 && item < 385;
});
return [...new Set(rng)].join(",") === str;
};
const arr = ["1, 2, 3, 5, 6, 7, 9",
"1-3, 5-7, 9",
"1-385",
"1-384",
"2, 2, 6",
"2,",
"1, 2, 3, 4, 15, 6, 7, 384",
"1, 2, 3, 4, 15, 6, 7, 385",
"0, 2, 3"
]
const res = arr.map(str => ({ [str]: isValid(str) }));
console.log(res)
Upvotes: 4
Reputation: 6390
You can try this-
function isValid(str) {
let lower = 1, upper = 384;
// Removing the unnecessary spaces
str = str.replace(/\s/g, '');
// Split the string by comma (,)
const nums = str.split(',');
const track = {};
// Visit the numbers
for (const num of nums) {
// Check if any number contains a dash (-)
if (/\-/.test(num)) {
// If has dash then split by dash and get the upper and lower bounds.
const [l, u] = num.split('-').map(x => x * 1);
// Visit from lower to upper bound
for (let i = l; i <= u; i++) {
// If any number of the range doesn't exceed the upper
// or lower bound i.e. [1, 384] range and did not
// appear before then track this number.
// otherwise return false i.e. mark it as invalid.
if (i >= lower && i <= upper && track[i] === undefined) {
track[i] = true;
} else {
return false;
}
}
} else {
// Checking again if it exceed the range [1, 384] or appears before.
if (num * 1 >= lower && num * 1 <= upper && track[num] === undefined) {
track[num] = true;
} else {
return false;
}
}
}
// If everything okay then return true, i.e. valid.
return true;
}
const strs = [
'1, 2, 3, 5, 6, 7, 9',
'1-3, 5-7, 9',
'2, 2, 6',
'2,',
'1, 2, 3, 4, 15, 6, 7, 385',
'1-4, 3, 7-9, 10',
'1-100, 102, 123',
'1-100, 102, 99'
];
for (const str of strs) {
console.log(str + ' => ' + (isValid(str) ? 'Valid': 'Invalid'));
}
.as-console-wrapper{min-height: 100%!important; top: 0}
Upvotes: 1
Reputation: 9368
This is not a very good use case for using just regular expressions. You need to apply some logic in addition to using regular expressions.
For eg.
,
-
in betweenUpvotes: 1