[英]RegEx validation for only empty space; should accept space between words/characters
I'm writing reg ex for UI validation for following requirements: 我正在为以下要求编写reg ex进行UI验证:
Here's my reg ex: 这是我的前任:
Validators.pattern('^[A-Za-z0-9? ,_-]+$')
Now except '#4', everything else is working fine. 现在,除了“#4”之外,其他所有东西都工作正常。 It's just accepting only white space; 它只接受空白; whereas as per requirement white space can be allowed only in between characters. 而根据要求,只能在字符之间使用空格。
The regex becomes heavier, but you can do this: 正则表达式变得更重,但是您可以这样做:
Validators.pattern('^[A-Za-z0-9?,_-](\s?[A-Za-z0-9?,_-]+)*$')
This ensures that: 这样可以确保:
[optional whitespace][at least one valid non-whitespace character]
包含任何序列的[optional whitespace][at least one valid non-whitespace character]
So that these match: 使这些匹配:
And these don't: 而且这些没有:
Note: I used your pattern, which allows ?
注意:我使用了您的模式,这允许?
, ,
and -
which are not included in your second requirement. , ,
和-
其中不包括在你的第二个要求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.