Home > Blockchain >  Regex to match on capital words, with some exceptions in javascript
Regex to match on capital words, with some exceptions in javascript

Time:01-26

Consider the following strings which would match:

  • HELLO WORLD HOW ARE YOU?
  • HELLO world HOW are YOU?
  • I am a SERIES OF WORDS that are capital
  • I am INVALID.

While these would not match:

  • Hello TV // (tv is not a word)
  • Come to my BBQ at my 1200 SQ.FT House.

The rule is very simple: No all capital words. (abbreviations are fine, words are not)

Currently I have:

^(?!.*\b[A-Z]{2,}\b).*$

this is the closest question but it is almost opposite of what I am trying to do.

I am not good with regex at all, I have tried various online tools and this is as close as I got. I believe the issue lies with the fact it should be something like \w or \W as I want words, not individual characters and not abbreviations like TV, BBQ, SQ.FT and so on, they have to be full on words as indicated above.

CodePudding user response:

Regular Expressions on their own cannot do this. You would need a separate dictionary with a list of all words that you do not want to be matched, then rule them out afterwards.

  • Related