简体   繁体   中英

Detecting All Caps in a string

I am having trouble trying to write a regular expression in JavaScript that can detect a whole word of at least 2 characters that is typed in all CAPS.

This is what I have tried and it seems to work .

/\b[^\Wa-z0-9_]+\b/

however, I will detect if a user starts a string "I like you."

Since, I is capitalized it returns true, hence why I want to only detect words that are all caps of more then 2 letters.

Your character group is needlessly complicated. If you simply want capital letters, why not use [AZ] ?

To restrict it to words of >= 2 letters, use {2,} instead of + as quantifier:

/\b[A-Z]{2,}\b/
var matches = ("hoi HOW are YOU doing?").match(/\b([A-Z]{2,})\b/g);
console.log(matches);  // ["HOW", "YOU"]

试试这个你的正则表达式:

/\b[A-Z]{2,}\b/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM