简体   繁体   English

AngularJs ng-pattern regex

[英]AngularJs ng-pattern regex

I am struggling writing a Regex for angular ng-pattern to validate a form input field.An example of the possible inputs are below 我正在努力为角度ng-pattern编写正则表达式来验证表单输入字段。下面可能的输入示例如下

Valid input: 有效输入:

@tag1,@tag2,@tag3
@tag1, @tag2, @tag3
@tag1

Invalid Input: 输入无效:

@tag 1
@tag 1, @tag2, @tag  -3
@ta g1, @t ag2

Basically it should allow comma and comma whitespace, but not allow whitespace in between tags. 基本上它应该允许逗号和逗号空格,但不允许在标签之间使用空格。

I have written a REGEX that matches all the invalid input, but when I use this in ng-pattern it does not do the correct validation. 我写了一个匹配所有无效输入的REGEX,但是当我在ng-pattern中使用它时,它没有做正确的验证。

Regex Built: /((^, @|@\\s+)|@*[a-zA-Z0-9]* +[a-zA-Z0-9])/g Link: https://regex101.com/r/HMVdLD/1 正则表达式内置: /((^, @|@\\s+)|@*[a-zA-Z0-9]* +[a-zA-Z0-9])/g链接: https//regex101.com/ R / HMVdLD / 1

Any help is much appreciated! 任何帮助深表感谢!

I suggest defining a pattern to match valid inputs, like 我建议定义一个匹配有效输入的模式,比如

ng-pattern="/^@[a-zA-Z0-9]+(?:,\s*@[a-zA-Z0-9]+)*$/"

See the regex demo 请参阅正则表达式演示

If you want to disallow leading and trailing whitespace, add ng-trim="false" . 如果要禁止前导和尾随空格,请添加ng-trim="false"

Pattern details : 图案细节

  • ^ - start of string ^ - 字符串的开头
  • @ - a literal @ symbol @ - 文字@符号
  • [a-zA-Z0-9]+ - 1+ alphanumerics [a-zA-Z0-9]+ - 1+字母数字
  • (?:,\\s*@[a-zA-Z0-9]+)* - 0+ sequences of: (?:,\\s*@[a-zA-Z0-9]+)* - 0+序列:
    • , - comma , - 逗号
    • \\s* - 0+ whitespaces \\s* - 0+空格
    • @[a-zA-Z0-9]+ - same as above @[a-zA-Z0-9]+ - 与上述相同
  • $ - end of string. $ - 结束字符串。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM