允许只有首字母大写的单词的正则表达式?

Regex for allowing words which have only first letter as uppercase?

我正在尝试制作一个正则表达式来验证所有单词的首字母是否大写。前任 像 Fruits 是允许的,但 fruits 是不允许的。 也允许美国,但不允许美国

此外,我不想只允许 space 和点。

我已经创建了正则表达式,不允许只允许 space 和点分开,但我希望它们在一个正则表达式中,更高的要求是他们实现这个的任何方法吗??

我会添加我目前尝试过的内容。

请提供任何帮助。


    $.validator.addMethod("first_letter", function(value,element)
    {
        return this.optional(element) || ^[A-Z].*i.test(value);  // 1.) this is what i am trying to check if first letter of every word is uppercase or not  and can this be combined with the 2.) validation i.e "characters" 
    }, "First Letter Should be Capital !!");

    $.validator.addMethod("characters", function(value,element)
    {
        return this.optional(element) || /^[a-zA-Z.*\s]*$/i.test(value);  // 2.) For only Allowing Alphabets
    }, "Only Characters are Allowed !!");
    
    $.validator.addMethod("dot", function(value,element)
    {
        return this.optional(element) || /^[^.]*$/i.test(value);  // 3.) For Not Allowing Dot Characters
    }, "Dot chatarcters are not Allowed !!");
    
    $.validator.addMethod("only_space", function(value,element)
    {
        return this.optional(element) || /^[.*\S.*]/i.test(value); // 4.) For Not Allowing Only Spaces
    }, "Only Spaces are not Allowed !!");

所有验证

    $.validator.addMethod("characters", function(value,element)
    {
        return this.optional(element) || /^[a-zA-Z.*\s]*$/i.test(value);
    }, "Only Characters are Allowed !!");
    
    $.validator.addMethod("dot", function(value,element)
    {
        return this.optional(element) || /^[^.]*$/i.test(value);
    }, "Dot chatarcters are not Allowed !!");
    
    $.validator.addMethod("only_space", function(value,element)
    {
        return this.optional(element) || /^[.*\S.*]/i.test(value);
    }, "Only Spaces are not Allowed !!"); 

    $.validator.addMethod("first_letter", function(value,element)
    {
        return this.optional(element) || ^[A-Z][a-z]*(?: [A-Z][a-z]*)*$/ i.test(value);
    }, "First Letter Should be Capital !!");    // your answer , if i am using it as it is then also its not working and other validations above it are also not working.

$.validator.addMethod("characters", function(value,element)
    {
        return this.optional(element) || /^[A-Z][a-zA-Z.*\s] (?: [A-Z][a-z]*)*$/i.test(value);
    }, "Only Characters are Allowed !!");    // what i tried 

$.validator.addMethod("first_letter", function(value,element)
    {
        return this.optional(element) || /^([A-Z] [a-z] * (([A-Za-z]\s)?[a-z]*)*)(\s+\S+\s*)*$/i.test(value);
    }, "First Letter Should be Capital !!");    // I also tried like this.

这样的模式看起来像:

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

说明

  • ^ 字符串开头
  • [A-Z][a-z]* 匹配单个大写字符和可选的小写字符
  • (?: [A-Z][a-z]*)* 可选择重复匹配一个space 和前面一样
  • $ 字符串结束

此模式限制字符 A-Z 作为第一个字符,后跟小写字符,其中“单词”之间可以只有 space。

查看 regex101 上的演示。

const regex = /^[A-Z][a-z]*(?: [A-Z][a-z]*)*$/;
[
  "Fruits",
  "fruits",
  "United States",
  "United states"
].forEach(s => console.log(`${s} --> ${regex.test(s)}`));

更广泛的匹配可以以大写字符开头,后跟可选的非白色space字符(也可以再次匹配大写字符)。

在重复部分,您可以使用 \s+ 来匹配 1 个或多个白色 space 字符(也可以匹配换行符)。

^[A-Z]\S*(?:\s+[A-Z]\S*)*$

regex101 上查看另一个演示。