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