简体   繁体   English

如何在“/”之前和之后生成最多 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 "/"

I have to generate a regex with following conditions我必须生成具有以下条件的正则表达式

  • Max of 3 numeric chars before and after the "/" Max of 7 chars “/”前后最多 3 个数字字符最多 7 个字符
  • including "/" and no other special chars & decimals allowed包括“/”并且不允许其他特殊字符和小数
  • Numbers before the "/" should be greater than numbers after the "/" “/”前的数字应大于“/”后的数字

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.

相关问题 正则表达式:只能有1个空格,但不能开头,没有数字/特殊字符,空格之前或之后的任何字符串都必须至少有2个字母字符 - Regex: Only 1 space but not at the start, no numbers/special characters, any string before or after space needs to have at least 2 alphabetic chars 每秒生成0到100之间的递增随机数,其中新数字应大于之前的nymber - Generate incremented random numbers between 0 to 100 every second where new number should be greater than the previous nymber 检查小数点前的4个数字和小数点后的1个数字 - Check for 4 numbers before decimal and 1 number after decimal Javascript - 如何在正则表达式中搜索大于 x 的数字 - Javascript - How to search for numbers greater than x in regex RegEx:如何匹配所有大于 49 的数字? - RegEx: How can I match all numbers greater than 49? RegEx在Javascript中替换大于变量的数字 - RegEx replacing numbers greater than variable in Javascript 正则表达式:匹配大于或等于 1001 的数字 - RegEx: Match numbers greater or equal than 1001 正则表达式-不在“-”,“。”,“ /”之后的所有数字 - regex - all numbers that are not after “-”, “.”, “/” 如何在 javascript 中生成数字/字符序列? - How to generate sequence of numbers/chars in javascript? 如何调整我的正则表达式以在点出现后只允许最多两个数字 - how to adjust my regex to only allow max two numbers after dot appears
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM