我试图验证一个逗号分隔的编号1-384的列表唯一(不重复)。
即
<代码>H191,2,3,4,15,6,7,385是无效的,因为最后的数字超过384h 210f 211
我尝试了以下RegEx模式,但还不够:
/^(?!.*(\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]))*$/发布于 2021-01-07 14:41:15
你可以试试这个-
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}
发布于 2021-01-07 14:46:40
一个过滤器和设置可能更容易在眼睛上。
"1-385“也是假的吗?
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)
发布于 2021-01-07 14:40:46
对于只使用正则表达式来说,这不是一个很好的用例。除了使用正则表达式之外,还需要应用一些逻辑。
就像。
,
-在
https://stackoverflow.com/questions/65614163
复制相似问题