[英]RegEx string start/end/middle one time match
I'm trying to validate input
field with regex.我正在尝试使用正则表达式验证
input
字段。 Task is: input
field can (optional) contain only one special character *
at start or middle or at the end of string.任务是:
input
字段可以(可选)在字符串的开头或中间或结尾仅包含一个特殊字符*
。 Here is my regex:这是我的正则表达式:
^(?!.?\*.*\()[\w*]+$
Problem is: string still valid to multiple *
and can't figure out what i'm doing wrong.问题是:字符串对多个
*
仍然有效,并且无法弄清楚我做错了什么。
You may use您可以使用
^(?!(?:[^*]*\*){2})[\w*]+$
See the regex demo查看正则表达式演示
Details细节
^
- start of string ^
- 字符串的开头(??(::[^*]*\*){2})
- no two not necessarily consecutive asterisks allowed in the string (??(::[^*]*\*){2})
- 字符串中不允许有两个不一定连续的星号[\w*]+
- one or more letters, digits, _
or *
chars [\w*]+
- 一个或多个字母、数字、 _
或*
字符$
- end of string $
- 字符串结尾
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.