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

如何选择文本中超过 1 个大写字符的所有单词?我设法用这一行选择某个单词:

(?<![a-z])word(?![a-z])

但我不知道如何选择像这样的词SElect, SeLeCt, SelecT, seleCT, selEcT


holdtom
浏览 101回答 3
3回答

慕田峪4524236

您可以使用模式来断言右侧的内容是“单词”,并匹配由可选的大写和小写字符包围的 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正则表达式演示

慕容3067478

const regex = /([a-z]*[A-Z]|[A-Z][a-z]*){2,}\b/gconst str = "SEEEEect, SeLeCt, SelecT, seleCT, selEcT select, seleCT, selEcT select, donselect"const match = str.match(regex)console.log(match)

子衿沉夜

我还建议一个完全 Unicode 正则表达式:/(?<!\p{L})(?:\p{Ll}*\p{Lu}){2}\p{L}*(?!\p{L})/gu见证明。解释:--------------------------------------------------------------------------------&nbsp; (?<!&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;look behind to see if there is not:--------------------------------------------------------------------------------&nbsp; &nbsp; \p{L}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; any Unicode letter--------------------------------------------------------------------------------&nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end of look-behind--------------------------------------------------------------------------------&nbsp; (?:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; group, but do not capture (2 times):--------------------------------------------------------------------------------&nbsp; &nbsp; \p{Ll}*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;any lowercase Unicode letter (0 or more&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;times (matching the most amount possible))--------------------------------------------------------------------------------&nbsp; &nbsp; \p{Lu}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;any uppercase Unicode letter--------------------------------------------------------------------------------&nbsp; ){2}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end of grouping--------------------------------------------------------------------------------&nbsp; \p{L}*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;any Unicode letter (0 or more&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;times (matching the most amount possible))--------------------------------------------------------------------------------&nbsp; (?!&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; look ahead to see if there is not:--------------------------------------------------------------------------------&nbsp; &nbsp; \p{L}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;any Unicode letter--------------------------------------------------------------------------------&nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end of look-aheadJavaScript: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));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript