3 回答
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
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)
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));
添加回答
举报