简体   繁体   English

正则表达式:仅一个下划线实例

[英]RegEx: Only one instance of underscore

I'm looking for a regex pattern that will accept the following: 我正在寻找将接受以下内容的正则表达式模式:

[foo_bar] or {foo_bar} [foo_bar]{foo_bar}

Where: 哪里:

  • either [ ] or { } acts as wrappers [ ]{ }充当包装器
  • foo and bar can be anything except _ repeated between 1 to n times foobar可以是除_以外的任何东西,重复1至n次
  • _ should only appear once and should be between foo and bar _应该只出现一次,并且应该在foobar之间

    Sample matches: 匹配样本:

  • [1Ab_23c]
  • {abcDefGhjk_Z}
  • {1_abcDefGhjk}

    Sample mismatches: 样品不匹配:

  • {1Ab_23c]
  • {_1Abde_fg4567_2_}
  • {1Abde_fg4567_2}
  • 1Ab_23c

    This is what I have right now but I'm still missing the only one _ part. 这就是我现在所拥有的,但是我仍然缺少唯一的 _部分。

     ^{.+_.+}|\\[.+_.+]$ 

    I'm guessing that a (?!_).+ should somehow be there but I can't piece them together. 我猜一个(?!_).+应该以某种方式存在,但我无法将它们拼凑在一起。

  • You can use a complemented character set - [^_] (all but underscore): 您可以使用补充字符集 - [^_] (除下划线以外的所有字符 ):

     const pattern = /\\[[^_]+_[^_]+\\]|\\{[^_]+_[^_]+\\}/; // matches console.log(pattern.test('[1Ab_23c]')); console.log(pattern.test('{1Abdefg4567_2}')); console.log(pattern.test('{1Ab_23c}')); // mismatches console.log(pattern.test('{1Ab_23c]')); console.log(pattern.test('{_1Abde_fg4567_2_}')); console.log(pattern.test('{1Abde_fg4567_2}')); console.log(pattern.test('1Ab_23c')); 

    暂无
    暂无

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

    相关问题 如何让正则表达式只接受一个可选的下划线? - How to make regex accept only one optional underscore? 正则表达式仅用于小写字母和下划线 - regex for only lowercase letters and underscore JavaScript 中的正则表达式允许最多 10 位数字,数字之间只有一个下划线,并防止在前 5 位数字后输入下划线 - Regex in JavaScript which allow maximum 10 digits, only one underscore in between the digits and prevent entering underscore after the first 5 digits 正则表达式仅匹配某些字符或字符串,并且每个匹配仅一个实例? - Regex to match only certain characters or strings and only one instance of each? 如何编写正则表达式匹配 word_char 但只有零或一个下划线 - How to write a regex matching word_char but only zero or one underscore Javascript正则表达式只有字母,数字和下划线 - Javascript regex only alphabet, number and underscore 正则表达式javascript仅允许数字,字母和下划线 - Regex javascript allow only numbers, alphabets and underscore 仅当它在正则表达式中出现两次时,如何捕获字符串的一个特定实例? - How to capture one particular instance of a string only if it occurs twice in regex? 在javascript中包含字母数字字符和最多一个下划线符号的用户名的正则表达式 - Regex to username that contains alphanumeric characters and at most one underscore symbol in javascript 仅一个点“。”正则表达式 - Only one dot “.” regex
     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM