简体   繁体   中英

How to use logic operator in Regex x <= y in Javascript

Hello i have regex like this

^[\d]{4,5}([;]\d{4,5})*$|(^[\d]{4,4}([:]\d{4,5})$)|^[\d]{4,5}(\s[;]\s\d{4,5})*$|(^[\d]{4,4}(\s[:]\s\d{4,5})$)

I need effect like this:

  • 12345: 12345 - allowed

  • 12345: 1234 - not allowed

  • 12345: 12344 - not allowed

  • 1234: 1235 - allowed

  • 1234: 12345 - allowed

Generally - left <= right

Rest can be stay

Someone know how to resolve this problem?

The problem you describe cannot be described by a regular language , as such I doubt this can be solved with a regex. You could however solve this easily in JS:

 const [a, b] = str.split(":");
 if(+a <= +b) /*...*/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM