你可以检查句子中的每个单词是否以大写字母开头并以”, “结尾吗?

huangapple go评论61阅读模式
英文:

How can I check if every word in a sentence starts with a capital letter and ends with ", "?

问题

我有一个输入类型为文本的元素,我想允许用户输入职业名称。我希望遵循以下格式:"职业1, 职业2, 职业3"。也就是说,以大写字母开头,如果有其他值,则单词之间应该用逗号和一个空格分隔。我已经尝试了HTML模式和PHP函数,但不幸的是,它们不能按照我想要的方式工作。重要的是,用户只能输入一个值,那么它应该看起来像"职业1",单词后面没有空格和逗号符号。前面的示例也适用于这个条件。

提前感谢。

英文:

I have an input type text where I would like to allow the user to type profession names. I want to be in this scheme "Profession1, Profession2, Profession3".That is, start with a capital letter and if there are other values ​​then the word should be followed by a "," and a single space. I've tried html patterns and php functions but unfortunately it doesn't work as I would like. The important thing is that the user can only enter one value, then it should look like "Profession1" without the spaces after the word and the "," symbol. The last condition also applies to the previous example.

Thanks in advance

答案1

得分: 0

以下是您要的代码的翻译部分:

function match(str) {
    let list = str.split(' ');
    for (let i = 0; i < list.length; i++) {
        if (list[i].charCodeAt(0) > 90 || list[i].charCodeAt(0) < 65) {
            return false;
        }
    }
    for (let i = 0; i < list.length - 1; i++) {
        if (!(list[i][list[i].length - 1] == ",")) {
            return false;
        }
    }
    return list;
}

当传递包含以逗号分隔的单词并且每个单词以大写字母开头的字符串时,它返回列表。否则,它返回false。

编辑:在评论中明确要求后,以下是更新后的函数:

function match(str) {
    let list = str.split(',');

    // 检查每个职业的开头是否有大写字母
    for (let i = 0; i < list.length; i++) {
        if (list[i].trim().charCodeAt(0) > 90 || list[i].trim().charCodeAt(0) < 65) {
            return false;
        }
    }

    // 检查每个逗号后面是否有空格
    for (let i = 1; i < list.length; i++) {
        if (!(list[i][0] == " ")) {
            return false;
        }
    }

    // 去除每个职业的空白
    l = list.map((profession) => {
        return profession.trim();
    })
    return l;
}

希望这对您有帮助。

英文:

For simplicities sake, the below solution does not use regex.

function match(str) {
    let list = str.split(&#39; &#39;);
    for (let i = 0; i &lt; list.length; i++) {
        if (list[i].charCodeAt(0) &gt; 90 || list[i].charCodeAt(0) &lt; 65) {
            return false;
        }
    }
    for (let i = 0; i &lt; list.length - 1; i++) {
        if (!(list[i][list[i].length - 1] == &quot;,&quot;)) {
            return false;
        }
    }
    return list;
}

When passed a string containing words separated by commas, with each word starting with a capital letter, it returns the list. Otherwise, it returns false.

Edit: After the clarification of requirements in the comments, below is the updated function.

function match(str) {
    let list = str.split(&#39;,&#39;);

    // Checks for capital letters at the start of each profession
    for (let i = 0; i &lt; list.length; i++) {
        if (list[i].trim().charCodeAt(0) &gt; 90 || list[i].trim().charCodeAt(0) &lt; 65) {
            return false;
        }
    }

    // Checks for spaces after every comma
    for (let i = 1; i &lt; list.length; i++) {
        if (!(list[i][0] == &quot; &quot;)) {
            return false;
        }
    }

    // Trim whitespace on each profession
    l = list.map((profession) =&gt; {
        return profession.trim();
    })
    return l;
}

huangapple
  • 本文由 发表于 2023年2月18日 20:03:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75493214.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定