描述
对于给定的由可见字符和空格构成的字符串 s,统计其中大写字母的个数。
字符串由 ASCII 码在 32 到 126 范围内的字符组成。您可以参阅下表获得其详细信息。
输入描述:
在一行上输入一个长度为 1≦len(s)≦250,由可见字符和空格构成的字符串 s 。
输出描述:
输出一个整数,表示字符串中大写字母的个数。
const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;
void (async function () {
// Write your code here
line = await readline();
let reg = new RegExp(/[A-Z]/, "g");
console.log(line.match(reg)?.length || 0);
})();
String.prototype.match()
match() 方法检索字符串与正则表达式进行匹配的结果。
const paragraph = 'The quick brown fox jumps over the lazy dog. It barked.';
const regex = /[A-Z]/g;
const found = paragraph.match(regex);
console.log(found);
// Expected output: Array ["T", "I"]