首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >验证数字范围

验证数字范围
EN

Stack Overflow用户
提问于 2021-01-07 14:23:46
回答 3查看 116关注 0票数 2

我试图验证一个逗号分隔的编号1-384的列表唯一(不重复)。

  • 1,2,3,5,6,6,7,9是有效的
  • 1- 3,5-7,9是有效的
  • 2,2,6是无效的
  • 2,是无效的

<代码>H191,2,3,4,15,6,7,385是无效的,因为最后的数字超过384h 210f 211

我尝试了以下RegEx模式,但还不够:

代码语言:javascript
复制
/^(?!.*(\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]))*$/
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2021-01-07 14:41:15

你可以试试这个-

代码语言:javascript
复制
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'));
}
代码语言:javascript
复制
.as-console-wrapper{min-height: 100%!important; top: 0}

票数 1
EN

Stack Overflow用户

发布于 2021-01-07 14:46:40

一个过滤器和设置可能更容易在眼睛上。

"1-385“也是假的吗?

代码语言:javascript
复制
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)

票数 4
EN

Stack Overflow用户

发布于 2021-01-07 14:40:46

对于只使用正则表达式来说,这不是一个很好的用例。除了使用正则表达式之外,还需要应用一些逻辑。

就像。

,

  • then检查单个表达式是否为-

  • 之间的远程表达式,然后验证各个表达式以检查其唯一性&边界
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65614163

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档