在 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
广告