简体   繁体   English

正则表达式 - 也允许空字符串

[英]Regular expression - also empty string allowed

I have the following regular expression... This one does not allow empty String. 我有以下正则表达式...这一个不允许空字符串。 How do I have to manipulate? 我该如何操纵?

var phoneReg = new RegExp(/^\+?[0-9]+(\([0-9]+\))?[0-9-]*[0-9]$/i);

Just use the "zero or one" operator ? 只需使用“零或一”运算符? , such as: , 如:

/^(?:regex)?$/
           ^

(?:regex) simply makes the concerned ground uncapturable (ie no need to change your $X , \\X or ?X indexes if any). (?:regex)只是让有关的地面无法控制 (即,如果有的话,无需更改你的$X\\X?X索引)。

I believe that the following would work: 我相信以下内容可行:

/^$|^\+?[0-9]+(\([0-9]+\))?[0-9-]*[0-9]$/i

I've inserted ^$| 我插入了^$| at the beginning of your regex. 在正则表达式的开头。 The pipe ( | ) is called an "alternation" indicator and says that either the expression before it or after it must match. 管道( | )被称为“交替”指示符,表示它之前或之后的表达式必须匹配。 ^$ will simply match a string with no characters between the start and end of input. ^$将简单地匹配输入的开始和结束之间没有字符的字符串。 The result: an empty string or the original expression. 结果:空字符串或原始表达式。

You can put a ^$| 你可以放一个^ $ | in front which in which ^ = beginning, $ = end, | 在前面哪个^ =开头,$ =结束,| = or this other thing to the right. =或者右边的其他东西。

var phoneReg = new RegExp(/^$|^\+?[0-9]+(\([0-9]+\))?[0-9-]*[0-9]$/i);

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

相关问题 正则表达式 - 匹配除+之外的任何字符,空字符串也应匹配 - Regular Expression - Match any character except +, empty string should also be matched JavaScript中的正则表达式不仅不允许字段中包含空格,而且允许包含空格的字符串,还允许包含空字段 - Regular expression in javascript to not allow only whitespaces in a field but allow string with whitespaces and also allow empty field Javascript:正则表达式中的空字符串匹配 - Javascript : Empty String Match In Regular Expression 正则表达式的模式,它将消除空字符串 - pattern for regular expression that will eliminate empty string 与模式匹配或为空字符串的正则表达式 - Regular expression which matches a pattern, or is an empty string 验证字符串的正则表达式以字符开头并包含允许的字符 - Regular expression to validate a string starts with a char and contains allowed chars Javascript正则表达式-仅过滤整数字符串,也为负数 - Javascript Regular Expression - Filter only integer string also negative 正则表达式是空的空格 - regular expression for is empty space 使用正则表达式拆分字符串,使其成为没有空元素的数组 - Splitting string with regular expression to make it array without empty element 为什么使用正则表达式。*? 只匹配JavaScript中的空字符串? - why regular expression .*? only match an empty string in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM