简体   繁体   English

什么是允许空白或数值的正确正则表达式

[英]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.

相关问题 正则表达式允许负值 - Regex to allow negative values 正则表达式:允许多封电子邮件,以;分隔 (分号)并允许空白/空值 - Regex: Allow multiple emails separated by ; (semicolon) AND allow blank/empty value 非数字的RegEx +仅允许单个“。”进行输入验证 - RegEx for non-numeric + allow only single “.” for input validation 正则表达式,用于包含特殊字符(-)的字母数字,并且最多只能包含4个数字字符 - Regex for alphanumeric with special character (-) and allow only maximum of 4 numeric characers 正则表达式避免使用数字和特殊字符,并在单词之间留出空格 - Regex avoid numeric and special characters and allow space between words 正则表达式只允许数字,第一个位置一个“$”和一个小数 - javascript - Regex allow only numeric, one '$' in first position and one decimal - javascript javascript regex 允许至少一个特殊字符和一个数字 - javascript regex to allow at least one special character and one numeric jQuery正则表达式替换非数字字符,但允许加 - jquery regex replace non numeric characters but allow plus jQuery - JavaScript 正则表达式只允许数字和减号作为第一个字符 - jQuery - JavaScript regex allow only numeric and minus sign as first character 正则表达式允许负值,最多4个小数位 - Regex to allow negative values with maximum of 4 decimal places
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM