在 JavaScript 中反转所有字母字符


问题

我们要求编写一个 JavaScript 函数,该函数接收字符串 str。我们的函数的任务是反转它,省略所有非字母字符。

示例

以下为代码 −

 在线演示

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));

Learn JavaScript in-depth with real-world projects through our JavaScript certification course. Enroll and become a certified expert to boost your career.

输出

elpmaxe

更新时间:17-Apr-2021

87 次浏览

启动您的职业生涯

完成课程即可获得认证

开始
广告