How can I use regex to match a word or groups of words that are in all capital letters?
I believe I've solved the problem half way, although it may not be the right way.
I am trying to catch one word in all caps, or two or three - basically if they're in succession I want them captured as a group, not as each word itself.
e.g.:
"HAPPY BIRTHDAY TOMMY" wouldn't match and return [0] -> HAPPY, [1] -> BIRTHDAY, [2] -> TOMMY
, but the whole group, such as [0] -> HAPPY BIRTHDAY TOMMY
.
The code I'm using below matches "HAPPY BIRTHDAY" together, or just "TOMMY", but not everything together.
[A-Z]{1,}\s[A-Z]{1,}|\b[A-Z]{1,}\b
You can use the regex:
(?=[A-Z])([A-Z\s]+)
See it
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With