How to count uppercase, lowercase, special characters, and numeric values using RegEx in JavaScript

1 Answer

0 votes
function countCharacters(text) {
    const uppercase = (text.match(/[A-Z]/g) || []).length;
    const lowercase = (text.match(/[a-z]/g) || []).length;
    const digits    = (text.match(/\d/g) || []).length;
    const special   = (text.match(/[^A-Za-z0-9]/g) || []).length;

    return { uppercase, lowercase, digits, special };
}

const s = "Programming&AI@2026!";

const { uppercase, lowercase, digits, special } = countCharacters(s);

console.log("Uppercase:", uppercase);
console.log("Lowercase:", lowercase);
console.log("Digits:", digits);
console.log("Special characters:", special);



/*
run:

Uppercase: 3
Lowercase: 10
Digits: 4
Special characters: 3

*/

 



answered Feb 20 by avibootz

Related questions

...