(Js) Leetcode 520. Detect capital letters

topic:

Given a word, you need to determine whether the capitalization of the word is correct.

We define that the capitalization of words is correct in the following situations:

All letters are capitalized, such as "USA".
None of the letters in a word are capitalized, such as "leetcode".
If the word contains more than one letter, only capitalize the first letter, such as "Google".
Otherwise, we define the word incorrectly using capital letters.

Example 1:

Input: "USA"
Output: True
Example 2:

Input: "FlaG"
Output: False
Note: Input is a non-empty word consisting of uppercase and lowercase Latin letters.

Ideas:

Use regular

Code:

/**
 * @param {string} word
 * @return {boolean}
 */
var detectCapitalUse = function(word) {
    const reg = /^[A-Z]+$|^[a-z]+$|^[A-Z][a-z]+$/;
    return reg.test(word);
};

operation result:

Guess you like

Origin blog.csdn.net/M_Eve/article/details/112854766