[英]Can this regex be simplified? Desired pattern is words with upper case
I have the following JavaScript regular expression.我有以下 JavaScript 正则表达式。 I'm wondering if there is a way to simplify or improve it.
我想知道是否有办法简化或改进它。
Here's my existing RegExp, which doesn't test for new lines, or the beginning of the string:这是我现有的 RegExp,它不测试新行或字符串的开头:
/([^0-9a-z+=%$#?!&<>;()@* -,.\/{}\^\[\]\\]+)$/
Here's what I've tried since words are only alphas and underscores, but it says new lines are valid, as are special characters.这是我尝试过的,因为单词只是字母和下划线,但它说新行是有效的,特殊字符也是如此。
/\b\w[^a-z0-9\n]+\b/
I am trying to have words with only uppercase alphas and underscores, with underscores only after an alpha.我试图让单词只有大写字母和下划线,下划线仅在字母之后。
Valid input would be:有效的输入是:
ERIS_TEST_GROUP_NAME
JENNIFER_AD_GROUP_NAME
PSEUDO_TEST_TEAM
TEST_GROUP
Invalid input would be anything with new lines or special characters, lower case characters, or starting with the underscore:无效输入可以是包含换行符或特殊字符、小写字符或以下划线开头的任何内容:
_JEN_TEST_GROUP
234*((_&&*^
ab^*(_EWRR)
e_RERE_^&)(*$#$#@()\\
What you have was close,你所拥有的很接近,
\w matches just a single character. \w 只匹配一个字符。
If you'd like to match more than one you can do it with '+'如果你想匹配多个,你可以用 '+'
/\b\w+[^a-z0-9\n]+\b/
[AZ]+ will match more than one uppercase characters [AZ]+ 将匹配多个大写字符
so you could try something like this:所以你可以尝试这样的事情:
/\b[A-Z]+(_[A-Z]+)*\b/
Looking at what you tried I think you might want to test an multiline input.查看您尝试过的内容,我认为您可能想要测试多行输入。 If so, you can try:
如果是这样,您可以尝试:
^(?=.)(?:\n?[A-Z]+(?:_[A-Z]+)*)+$
^
- Start string anchor. ^
- 开始字符串锚。(?=.)
- Positive lookahead for a character other than newline. (?=.)
- 对换行符以外的字符进行正向前瞻。(?:
- Open 1st non capture group. (?:
- 打开第一个非捕获组。
\n?
- An optional newline character. [AZ]+
- Any uppercase alpha at least once. [AZ]+
- 任何大写字母至少一次。(?:
- Open 2nd non capture group. (?:
- 打开第二个非捕获组。
_[AZ]+
- Underscore followed by at least one uppercase alpha. _[AZ]+
- 下划线后跟至少一个大写字母。)*
- Close 2nd non capture group and match it zero or more. )*
- 关闭第二个非捕获组并将其匹配零个或多个。)+
- Close 1st capture group and match at least once. )+
- 关闭第一个捕获组并至少匹配一次。$
- End string anchor. $
- 结束字符串锚。 And if not, then I think you should go with the option mentioned in the comments by @Anubhava如果没有,那么我认为您应该使用@Anubhava 评论中提到的选项 go
Use利用
^[A-Z]+(?:_[A-Z]+)*(?:\n[A-Z]+(?:_[A-Z]+)*)*$
Explanation :说明:
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
[A-Z]+ any character of: 'A' to 'Z' (1 or more
times (matching the most amount possible))
--------------------------------------------------------------------------------
(?: group, but do not capture (0 or more times
(matching the most amount possible)):
--------------------------------------------------------------------------------
_ '_'
--------------------------------------------------------------------------------
[A-Z]+ any character of: 'A' to 'Z' (1 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
)* end of grouping
--------------------------------------------------------------------------------
(?: group, but do not capture (0 or more times
(matching the most amount possible)):
--------------------------------------------------------------------------------
\n '\n' (newline)
--------------------------------------------------------------------------------
[A-Z]+ any character of: 'A' to 'Z' (1 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
(?: group, but do not capture (0 or more
times (matching the most amount
possible)):
--------------------------------------------------------------------------------
_ '_'
--------------------------------------------------------------------------------
[A-Z]+ any character of: 'A' to 'Z' (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
)* end of grouping
--------------------------------------------------------------------------------
)* end of grouping
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.