简体   繁体   English

匹配大写单词的正则表达式,javascript 中有一些例外

[英]Regex to match on capital words, with some exceptions in javascript

Consider the following strings which would match:考虑以下匹配的字符串:

  • HELLO WORLD HOW ARE YOU?你好世界你好吗?
  • HELLO world HOW are YOU? HELLO world 你好吗?
  • 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.来我 1200 平方英尺的房子烧烤吧。

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.我认为问题在于它应该是 \w 或 \W 之类的东西,因为我想要单词,而不是单个字符,而不是像 TV、BBQ、SQ.FT 等缩写,它们必须是完整的单词多于。

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.您需要一个单独的字典,其中包含您不想匹配的所有单词的列表,然后将它们排除在外。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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