Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
220 views
in Technique[技术] by (71.8m points)

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

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.

/w[^a-z0-9
]+/

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 '+'

/w+[^a-z0-9
]+/

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

so you could try something like this:

/[A-Z]+(_[A-Z]+)*/

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...