亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

正則表達式文本中包含超過 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 關注
  • 173 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號