如何在“/”之前和之后生成最多 3 个数字字符的正则表达式,其中“/”之前的数字应大于“/”之后的数字

How to generate a regex with Max of 3 numeric chars before & after the "/" where Numbers before the "/" should be greater than numbers after the "/"

我必须生成符合以下条件的正则表达式

到目前为止我已经试过了

^\d{3}(\/\d{3})?$

我将如何修复正则表达式的验证 "Numbers before the "/" 应该大于 "/"" 之后的数字?

谁能帮帮我?

您将无法单独使用正则表达式来实现此目的(至少,不是那么容易或可读)- 相反,捕获两个数字部分,并检查第一位数字是否大于第二位:

const check = str => {
  const match = str.match(/^(\d{1,3})\/(\d{1,3})$/);
  if (!match) {
    return false;
  }
  const [, d1, d2] = match;
  return Number(d1) > Number(d2);
};

console.log(
  check('123/456'),
  check('456/123'),
  check('45/123'),
  check('12/45')
);

您可以分配两个变量 - 一个用于“/”之前的数字,一个用于之后。 像 A/B,然后使用 split() 函数以 '-' 作为分隔符,然后将两部分与 >.

进行比较

据我所知,Regex 中不能比较数值。