简体   繁体   English

如何使用正则表达式验证此类输入?

[英]How to validate such input using a regular expression?

Somebody's can help with this maybe by using regex.有人可以通过使用正则表达式来帮助解决这个问题。 It is necessary for the "useInput" function to have minutes as the first parameter and seconds as the second parametr, if input can be: "1 2" (two digits - minutes seconds), "1" (just a digit - minutes), "1 minutes 2", "2 seconds 1 ". “useInput” function 必须将分钟作为第一个参数,将秒作为第二个参数,如果输入可以是:“1 2”(两位数 - 分钟秒),“1”(只是一个数字 - 分钟) , "1 分钟 2", "2 秒 1"。

Not so necessary but if is not too hard: seconds = sec = second = s;没那么必要,但如果不是太难的话: seconds = sec = second = s; minutes = minute = min = m;分钟 = 分钟 = 分钟 = 米; Іnstead of "1 minutes" you can write "1 min" into "input" Іn 而不是“1 分钟”,您可以将“1 分钟”写入“输入”

 const input = " 2 seconds 1 minutes " const regexTimer = new RegExp(/([0-9]+)\s*minutes?|([0-9]+)\s*seconds?/, 'g') useInput(input.replace(regexTimer, '$1$2')).split(' '));//1 parameter is minute, 2 parametr is second function useInput(minutes, seconds){...};

You could use capture groups and pass those group values to the function.您可以使用捕获组并将这些组值传递给 function。

Check which group values are not empty, and based on that pass the parameters in the right order to the function useInput.检查哪些组值不为空,并在此基础上以正确的顺序将参数传递给 function useInput。

For the partial names of seconds and minutes you can use nested optional groups.对于秒和分钟的部分名称,您可以使用嵌套的可选组。

^\s*(?:(\d+)(?:\s*(\d+))?|(\d+)(?:\s+s(?:ec(?:onds?)?)?)?(?:\s+(\d+)(?:\s+m(?:in(?:utes?)?)?)?)?|(\d+)(?:\s+m(?:in(?:utes?)?)?)?(?:\s+(\d+)(?:\s+s(?:ec(?:onds?)?)?)?)?)\s*$

Regex demo正则表达式演示

 const useInput = (minutes, seconds) => console.log(`minutes: ${minutes? minutes: "n/a"} - seconds: ${seconds? seconds: "n/a"}`); const regex = /^\s*(?:(\d+)(?:\s*(\d+))?|(\d+)(?:\s+s(?:ec(?:onds?)?)?)?(?:\s+(\d+)(?:\s+m(?:in(?:utes?)?)?)?)?|(\d+)(?:\s+m(?:in(?:utes?)?)?)?(?:\s+(\d+)(?:\s+s(?:ec(?:onds?)?)?)?)?)\s*$/; [ "1", "1 2", "1 minutes 2", "4 seconds 3", "5 seconds", "6 minutes", "7 min", "8 sec", "9 s 10 m", "11 m 12 second", "20 9 sec", "30 s 44", "test" ].forEach(s => { let m = s.match(regex); if (m) { if (m) { if (undefined,== m[1]) useInput(m[1], m[2]) if (undefined,== m[3]) useInput(m[4], m[3]) if (undefined !== m[5]) useInput(m[5], m[6]) } } })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用正则表达式来验证中文输入? - How to use regular expression to validate Chinese input? 使用正则表达式验证整个输入 - Validate whole input with a regular expression 使用正则表达式验证用户输入以获取字符和数字不起作用 - Validate User input to take characters and numbers using regular expression not working 使用正则表达式验证输入中是否包含任何非数字 - Using a regular expression to validate whether input has any non digits in it 如何在客户端流星中使用自动格式化中的正则表达式来验证字段的输入值? - How to validate a field's input value using regular expression in autoform in meteor on client side? 如何验证仅通过 JavaScript 正则表达式输入的字母和空格 - How to validate a letter and whitespace only input via JavaScript regular expression 如何使用jQuery将正则表达式应用于输入? - How to apply Regular Expression to input using jQuery? 使用正则表达式验证javascript中通用字母的输入 - Validate input with regular expression for universal alphabets in javascript JavaScript正则表达式以验证输入,例如2.4 ** 6-0 - JavaScript regular expression to validate input such as 2.4**6-0 如何使用正则表达式验证多个电话号码 - How to validate multiple phone number using regular expression
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM