简体   繁体   English

正则表达式字符串开始/结束/中间一次匹配

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

相关问题 使用正则表达式匹配字符串的相同开始和结束字符 - Match the same start and end character of a string with Regex 仅在字符串的开头/结尾匹配正则表达式 - Match Regex Only at the Start/End of a String Javascript REGEX仅用于删除字符串开头和结尾的字符,而不是中间的字符 - Javascript REGEX for deleting char only in string start and end, not in the middle JavaScript RegEx:将不在字符串开头或结尾处的所有负号与货币输入匹配 - JavaScript RegEx: match all minuses that are not at start or end of string for currency input 正则表达式匹配没有开始和结束空格的字符串中的单词 - Regex match the words from a string without the start and end space 正则表达式:在另一个字符串中间匹配url字符串 - Regex: Match url string in the middle of another string 正则表达式在 Javascript 中的字符串末尾精确匹配一次出现的字符 - RegEx to match exactly one occurrence of character at the end of string in Javascript 正则表达式以匹配URL-字符串末尾的数字,但也匹配字符串开头的字符 - Regex to match URL - number at end of string, but also match characters at start of string 正则表达式允许一个逗号,而不是在 JavaScript 中字符串的开头或结尾 - regex to allow one comma and not at the start or end of string in JavaScript 如何从字符串开头获取与RegEx的匹配 - How can I get one match with RegEx from start of string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM