Can this regex be simplified? Desired pattern is words with upper case

Viewed 205

I have the following JavaScript regular expression. 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:

/([^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_^&)(*$#$#@()\\
3 Answers

What you have was close,

\w matches just a single character.

If you'd like to match more than one you can do it with '+'

/\b\w+[^a-z0-9\n]+\b/

[A-Z]+ will match more than one uppercase characters

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]+)*)+$

See the online demo

  • ^ - Start string anchor.
  • (?=.) - Positive lookahead for a character other than newline.
  • (?: - Open 1st non capture group.
    • \n? - An optional newline character.
    • [A-Z]+ - Any uppercase alpha at least once.
    • (?: - Open 2nd non capture group.
      • _[A-Z]+ - Underscore followed by at least one uppercase alpha.
      • )* - 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

Use

^[A-Z]+(?:_[A-Z]+)*(?:\n[A-Z]+(?:_[A-Z]+)*)*$

See proof

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
Related