[英]What is the right regex to allow blank or numeric values
I want to validate one variable named "Port" in my JavaScript to accept only either numerical digits or a blank value.我想在我的 JavaScript 中验证一个名为“Port”的变量,以仅接受数字或空白值。 I am not sure exactly what regex I can use to satisfy both conditions.
我不确定我可以使用什么正则表达式来满足这两个条件。 Can anyone suggest me logic to use under my if loop to validate this?
谁能建议我在 if 循环下使用的逻辑来验证这一点?
If you want to achieve your goal with regexes, you are looking for:如果您想使用正则表达式实现您的目标,您正在寻找:
const regex = /^\\d*$/; console.log(regex.test("")); // true console.log(regex.test("8080")); // true console.log(regex.test("4d54")); // false
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.