[英]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 "/"
I have to generate a regex with following conditions我必须生成具有以下条件的正则表达式
I have tried this so far到目前为止我已经尝试过这个
^\d{3}(\/\d{3})?$
How will I fix this validation on regex "Numbers before the "/" should be greater than numbers after the "/""?我将如何在正则表达式“/”之前的数字应该大于“/””之后的数字上修复此验证?
Can anyone please help me out?任何人都可以帮我吗?
You won't be able to achieve this with a regular expression alone (at least, not that easily or readably) - instead, capture both digit parts, and check that the first digits are greater than the second:您将无法单独使用正则表达式来实现这一点(至少,不是那么容易或可读) - 相反,捕获两个数字部分,并检查第一个数字是否大于第二个:
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') );
You can assign two variables - One for the digits before '/' and one for after.您可以分配两个变量 - 一个用于 '/' 之前的数字,另一个用于之后。 like A/B, then use split() function with '-' as the delimiter and then compare the two parts with >.
像 A/B,然后使用 split() 函数以 '-' 作为分隔符,然后将两部分与 > 进行比较。
As far as I know, numeric values cannot be compared in Regex.据我所知,不能在正则表达式中比较数值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.