【问题标题】:Checking if first character of each word is Uppercase检查每个单词的第一个字符是否为大写
【发布时间】:2021-08-19 21:00:31
【问题描述】:

我想检查字符串中每个单词的第一个字符是否为大写。 其他字符应为小写。

例如:

My Lady D'Arbanville => True
My Lady d'arbanville => False
My LADY D'Arbanville => False

我试过了:^[A-Z][a-z ']*$ 但不工作

【问题讨论】:

  • ^ 匹配字符串的开头,而不是单词的开头。
  • 使用\b 匹配单词边界。
  • 如何定义一个“词”?

标签: regex uppercase lowercase


【解决方案1】:

如果两个大写字符之间可以有一个单引号,您可以选择重复。

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

模式匹配:

  • ^ 字符串开始
  • [A-Z] 匹配 A-Z 范围内的字符
  • (?:'[A-Z])* 可以选择重复 ' 和一个字符 A-Z
  • [a-z]+ 匹配 a-z 范围内的 1+ 个小写字符
  • (?:非捕获组整体重复
    • [A-Z] 匹配一个大写字符
    • (?:'[A-Z])* 可以选择重复 ' 和一个大写字符
    • [a-z]+ 匹配 a-z 范围内的 1+ 个字符
  • )*可选重复非捕获组,前面有一个空格
  • $字符串结束

Regex demo

也匹配单个大写字符:

^[A-Z](?:'[A-Z])*[a-z]*(?: [A-Z](?:'[A-Z])*[a-z]*)*$

Regex demo

【讨论】:

  • 你好,如果没有引号或只有一个字符,它似乎不匹配。例如:黄飞鸿(0匹配)
  • @RyanDavidson 然后您可以将量词从+ 更改为*,如^[A-Z](?:'[A-Z])*[a-z]+(?: [A-Z](?:'[A-Z])*[a-z]*)*$ 见regex101.com/r/oNNHSY/1
【解决方案2】:

使用您显示的示例,请尝试以下正则表达式。

^[A-Z][a-z]+\s+[A-Z][a-z]+\s+[A-Z](?:'[A-Z])?[a-z]+$

Online demo for above regex

说明:为上述添加详细说明。

^[A-Z][a-z]+      ##Checking from starting of value if it starts from capital letter followed by 1 or more small letters here.
\s+               ##Matching 1 or more space occurrences here.
[A-Z][a-z]+       ##Matching 1 occurrence of capital letter followed by 1 or more small letters here.
\s+               ##Matching 1 or more space occurrences here.
[A-Z](?:'[A-Z])?  ##Matching single capital letter followed by optional ' capital letter here.
[a-z]+$           ##Matching 1 or more occurrences of small letters till end of value here.

【讨论】:

    【解决方案3】:

    检查否定条件要简单得多:

    \b[a-z]|\B[A-Z]
    

    如果此模式成功,则条件为假。

    【讨论】:

      【解决方案4】:

      我讨厌完全依赖正则表达式,很难隔离边缘情况或缩小普遍性。尽管 OP 的问题要求对某个目标使用正则表达式,但有更简单的方法可以实现这一目标并留出适应空间。

      下面的函数将传递一个string(例如"My Lady D'Arbanville")然后:

      1. 通过删除空格 (\s) 或 (|) 单引号 ('),

        .split() 将 string 转换为 array of strings。

         `My Lady D'Arbanville`.split(/\s|'/);
        

        返回:["My", "Lady", "D", "Arbanville"]

      2. .every() word 中的 array of strings 将由 function() 进行测试。每个测试的单词必须是true,以便将给定的string报告为true。

         ["My", "Lady", "D", "Arbanville"].every(function(word) {})
        
      3. function()将.test()每个word看下面是不是true:

        • 第一个字符是大写字母[A-Z]
        • 第一个字符后面的所有字符都是小写[a-z]+

        或|

        • \b单词\b 是一个大写字母[A-Z] 的单个字符
        /[A-Z][a-z]+|\b[A-Z]\b/.test(word)
        

      const titleCheck = (text) => {
        const wordArray = text.split(/\s|'/);
        console.log(wordArray);
        return wordArray.every(word => {
          const rgx = new RegExp(/[A-Z][a-z]+|\b[A-Z]\b/);
          return rgx.test(word);
        });
      };
      
      /* true
      Control 
      */
      console.log(titleCheck(`My Lady D'Arbanville`));
      
      /* false
      A word starting with a lower case letter
      */
      console.log(titleCheck(`My Lady D'arbanville`));
      
      /* false
      A word with any upper case letter that is
      NOT the first letter
      */
      console.log(titleCheck(`My LADY D'Arbanville`));
      
      /* false
      A single letter word that is lower case
      */
      console.log(titleCheck(`My Lady d'Arbanville`));

      【讨论】:

        猜你喜欢
        • 2018-07-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多