1/**
2 * @param {string} S
3 * @return {string}
4 */
5var reverseOnlyLetters = function(S) {
6 let reverseArr = [...S].filter(item => /[A-Za-z]/.test(item)).reverse();
7 return [...S]
8 .map(item => /[A-Za-z]/.test(item) ? reverseArr.shift() : item)
9 .join(‘‘);
10};
11
12ensureEqual(reverseOnlyLetters("ab-cd"), "dc-ba", ‘test1‘);
13ensureEqual(reverseOnlyLetters("a-bC-dEf-ghIj"), "j-Ih-gfE-dCba", ‘test2‘);
14ensureEqual(reverseOnlyLetters("Test1ng-Leet=code-Q!"), "Qedo1ct-eeLg=ntse-T!", ‘test3‘);
原文:https://www.cnblogs.com/rencoo/p/10137865.html