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));
添加回答
舉報