为了账号安全,请及时绑定邮箱和手机立即绑定

正则表达式文本中包含超过 1 个大写字符的所有单词

正则表达式文本中包含超过 1 个大写字符的所有单词

holdtom 2024-01-18 10:59:30
如何选择文本中超过 1 个大写字符的所有单词?我设法用这一行选择某个单词:(?<![a-z])word(?![a-z])但我不知道如何选择像这样的词SElect, SeLeCt, SelecT, seleCT, selEcT。
查看完整描述

3 回答

?
慕田峪4524236

TA贡献1875条经验 获得超5个赞

您可以使用模式来断言右侧的内容是“单词”,并匹配由可选的大写和小写字符包围的 2 个大写字符

(?<![a-zA-Z])[a-z]*[A-Z][a-z]*[A-Z][A-Za-z]*(?![a-zA-Z])

解释

  • (?<![a-zA-Z])断言左侧不是 a-zA-Z

  • [a-z]*[A-Z]匹配可选字符 az 后接 AZ 以匹配第一个大写字符

  • [a-z]*[A-Z]再次匹配可选字符 az 后跟 AZ 以匹配第二个大写字符

  • [a-zA-Z]*匹配可选字符 a-zA-Z

  • (?![a-zA-Z])断言右侧不是 a-zA-Z

正则表达式演示


查看完整回答
反对 回复 2024-01-18
?
慕容3067478

TA贡献1773条经验 获得超3个赞

const regex = /([a-z]*[A-Z]|[A-Z][a-z]*){2,}\b/g

const str = "SEEEEect, SeLeCt, SelecT, seleCT, selEcT select, seleCT, selEcT select, donselect"


const match = str.match(regex)

console.log(match)


查看完整回答
反对 回复 2024-01-18
?
子衿沉夜

TA贡献1828条经验 获得超3个赞

我还建议一个完全 Unicode 正则表达式:

/(?<!\p{L})(?:\p{Ll}*\p{Lu}){2}\p{L}*(?!\p{L})/gu

证明

解释

--------------------------------------------------------------------------------

  (?<!                     look behind to see if there is not:

--------------------------------------------------------------------------------

    \p{L}                  any Unicode letter

--------------------------------------------------------------------------------

  )                        end of look-behind

--------------------------------------------------------------------------------

  (?:                      group, but do not capture (2 times):

--------------------------------------------------------------------------------

    \p{Ll}*                 any lowercase Unicode letter (0 or more

                             times (matching the most amount possible))

--------------------------------------------------------------------------------

    \p{Lu}                   any uppercase Unicode letter

--------------------------------------------------------------------------------

  ){2}                     end of grouping

--------------------------------------------------------------------------------

  \p{L}*                   any Unicode letter (0 or more

                           times (matching the most amount possible))

--------------------------------------------------------------------------------

  (?!                      look ahead to see if there is not:

--------------------------------------------------------------------------------

    \p{L}                   any Unicode letter

--------------------------------------------------------------------------------

  )                        end of look-ahead

JavaScript:


const regex = /(?<!\p{L})(?:\p{Ll}*\p{Lu}){2}\p{L}*(?!\p{L})/gu;

const string = "SEEEEect, SeLeCt, SelecT, seleCT, selEcT select, seleCT, selEcT select, donselect";

console.log(string.match(regex));


查看完整回答
反对 回复 2024-01-18
  • 3 回答
  • 0 关注
  • 38 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信