[英]All words in text with more than 1 uppercase characters with regex
如何 select 文本中所有超过 1 个大写字符的单词? 我设法用这一行 select 某个词:
(?<?[az])word(?![az])
But I'm not sure how to select words like SElect, SeLeCt, SelecT, seleCT, selEcT
.
您可以使用模式来断言右侧是“单词”并匹配由可选大小写字符包围的 2 个大写字符
(?<![a-zA-Z])[a-z]*[A-Z][a-z]*[A-Z][A-Za-z]*(?![a-zA-Z])
解释
(?<![a-zA-Z])
断言左边不是 a-zA-Z[az]*[AZ]
匹配可选字符 az 后跟 AZ 匹配第一个大写字符[az]*[AZ]
再次匹配可选字符 az 后跟 AZ 匹配第二个大写字符[a-zA-Z]*
匹配可选字符 a-zA-Z(?![a-zA-Z])
断言右边不是 a-zA-Z const regex = /([az]*[AZ]|[AZ][az]*){2,}\b/g const 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
见证明。
说明:
--------------------------------------------------------------------------------
(?<! 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));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.