[英]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: 哪里:
[
]
or {
}
acts as wrappers [
]
或{
}
充当包装器 foo
and bar
can be anything except _
repeated between 1 to n times foo
和bar
可以是除_
以外的任何东西,重复1至n次 _
should only appear once and should be between foo
and bar
_
应该只出现一次,并且应该在foo
和bar
之间 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.