0

Hello I am having trouble trying to use Regex to check if each character in string is an alphabet.

First let me introduce the problem itself. There is a string mixed with special chars and alphabets and suppose to return the number of alphabets only.

My code/pseudo code for problem is :

//Create var to hold count;
var count = 0;
//Loop thru str
for(let char of str){
//Check if char is a alphabet 
    ***if(char === /[A-Za-z]/gi){***
    //if so add to count
    count ++;
}
//return count; 
    return count;
} 

How can I use Regex in a conditional statement to check if each char is an alphabet???? Please help!

Daniel Oh
  • 43
  • 7

1 Answers1

1

const pattern = /[a-z]/i
const result = [...'Abc1'].reduce((count,c) => pattern.test(c) ? count+1 : count, 0)

console.log(result) // 3
Ben Aston
  • 53,718
  • 65
  • 205
  • 331