Reversing all alphabetic characters in JavaScript


Problem

We are required to write a JavaScript function that takes in a string str. The job of our function is to reverse it, omitting all non-alphabetic characters.

Example

Following is the code −

 Live Demo

const str = 'exa13mple';
function reverseLetter(str) {
   const res = str.split('')
   .reverse()
   .filter(val => /[a-zA-Z]/.test(val))
   .join('');
   return res;
};
console.log(reverseLetter(str));

Output

elpmaxe

Updated on: 17-Apr-2021

60 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements