题解 | #统计字符#
统计字符
https://www.nowcoder.com/practice/539054b4c33b4776bc350155f7abd8f5
const readline = require('readline'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); const letters = /[a-zA-Z]/g; // 匹配大小写英文字母 const num = /\d/g; // 匹配0-9数字 const empty = /\s/g; // 匹配空格 rl.on('line', function (line) { // regx的match,匹配成功则返回对应匹配字符的数组集合,否则返回null,因此这里有一个兼容写法 let l = (line.match(letters) || []).length; let n = (line.match(num) || []).length; let e = (line.match(empty) || []).length; console.log(l) console.log(e) console.log(n) console.log(line.length - l - n - e) });