Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

520. 检测大写字母 #37

Open
pigpigever opened this issue Dec 31, 2019 · 0 comments
Open

520. 检测大写字母 #37

pigpigever opened this issue Dec 31, 2019 · 0 comments
Labels
字符串 关于字符串的题目 正则

Comments

@pigpigever
Copy link
Owner

解题思路

正则适用于字符串的匹配,根据题目的要求:

  • 全部字母都是大写,比如"USA"。
  • 单词中所有字母都不是大写,比如"leetcode"。
  • 如果单词不只含有一个字母,只有首字母大写, 比如 "Google"。

转化成正则的表达:

  • 匹配全部由大写字母组成的字符串
  • 匹配全部由小写字母组成的字符串
  • 匹配由首字母大写组成的字符串

代码

/**
 * @param {string} word
 * @return {boolean}
 */
var detectCapitalUse = function(word) {
    return /^[A-Z]+$|^[a-z]+$|^[A-Z][a-z]+$/.test(word)
};
@pigpigever pigpigever added 字符串 关于字符串的题目 正则 labels Dec 31, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
字符串 关于字符串的题目 正则
Projects
None yet
Development

No branches or pull requests

1 participant