簡體   English   中英

允許使用一個或多個連續大寫字母和數字的 PascalCased 的正則表達式

[英]Regex that allows PascalCased with one or more consecutive uppercase letters and numbers

我正在嘗試檢查用戶輸入的 PascalCased 名稱並且這有效,但我還想允許一個或多個連續的大寫字母,例如。 UNOrganization 並且還允許介於兩者之間的數字,例如。 A2B組織。

因此,應允許以下所有內容:ABCWord、A3BWord、OtherWord、Word3、Word3AB(最后不太可能,但如果可能的話)

  if (value.match(/^[A-Z][a-z]+(?:[A-Z][a-z]+)*$/)) {
    //logic here
  }

正則表達式有點超出我的理解范圍,解析字符串的邏輯對於我的需要來說太長了,我知道這可以用正則表達式在一行中完成,所以希望更精明的人可以幫助我。

采用

^[A-Z]+[a-z]*(?:\d*(?:[A-Z]+[a-z]*)?)*$

證明

如果您要求輸入字符串中至少有一個小寫字母:

^(?=.*[a-z])[A-Z]+[a-z]*(?:\d*(?:[A-Z]+[a-z]*)?)*$

解釋

--------------------------------------------------------------------------------
  ^                        the beginning of the string
----------------------------------------------------------------------------------------------------------------------------------------------------------------
 (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                         (matching the most amount possible))
--------------------------------------------------------------------------------
    [a-z]                    any character of: 'a' to 'z'
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  [A-Z]+                   any character of: 'A' to 'Z' (1 or more
                           times (matching the most amount possible))
--------------------------------------------------------------------------------
  [a-z]*                   any character of: 'a' to 'z' (0 or more
                           times (matching the most amount possible))
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (0 or more times
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    \d*                      digits (0-9) (0 or more times (matching
                             the most amount possible))
--------------------------------------------------------------------------------
    (?:                      group, but do not capture (optional
                             (matching the most amount possible)):
--------------------------------------------------------------------------------
      [A-Z]+                   any character of: 'A' to 'Z' (1 or
                               more times (matching the most amount
                               possible))
--------------------------------------------------------------------------------
      [a-z]*                   any character of: 'a' to 'z' (0 or
                               more times (matching the most amount
                               possible))
--------------------------------------------------------------------------------
    )?                       end of grouping
--------------------------------------------------------------------------------
  )*                       end of grouping
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
/^(?=.*[a-z])[A-Z]\d*[a-z]*(([A-Z\d]*[A-Z]|[A-Z][A-Z\d]*|\d+$)[a-z]*)*$/

似乎符合您的要求。 請參閱下面的測試。

這個想法是以大寫字母開頭,后跟可選數字和零個或多個小寫字母。 然后進入主組重復零次或多次。 該組匹配一個或多個大寫字母或數字后跟零個或多個小寫字母。 交替句柄允許數字位於末尾並不允許夾在兩個小寫字符之間的單獨數字。

 const pat = /^(?=.*[az])[AZ]\d*[az]*(([AZ\d]*[AZ]|[AZ][AZ\d]*|\d+$)[az]*)*$/; const tests = [ "UNOrganization", "A2BOrganization", "ABCWord", "A3BWord", "OtherWord", "Word3", "Word3AB", "a", "A", "Aa", "aA", "AAA", "aaa", "9A", "A4", "A4a", "AaA", "AaAa", "Aa1", "AaA1", "Aa1a", "", ]; const len = 2 + Math.max(...tests.map(e => e.length)); tests.forEach(e => console.log(`${`'${e}'`.padStart(len)} => ${pat.test(e)}`) );

另一種模式:

^[A-Z]+[A-Za-z]*(?:[0-9]+(?:[A-Z]+[A-Za-z]*)*)*$
  • [AZ]+[A-Za-z]* :至少一個大寫字母可選地跟在任何大小寫的字母序列之后,...

  • (?: ... )* : ... 可選地后跟一系列:

    • [0-9]+ : 一位或多位數字...

    • (?: ... )* :可選地后跟一系列:

      • [AZ]+[A-Za-z]* :至少一個大寫字母可選地跟在任何大小寫的字母序列之后

Regex101.com 工作示例(替換^$為單詞分隔符\b )。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM