Last active
March 18, 2018 02:51
-
-
Save SolidZORO/d30fdac96fd00af1be9b77e2c64ab9fa to your computer and use it in GitHub Desktop.
reverse char in string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const source = '(a,(b,(c,(d,(e,(f))))))'; | |
const sourceList = source.split(''); | |
const charList = []; | |
const keyList = []; | |
// Find Char & Key | |
sourceList.map((char, i) => { | |
if (/[a-z]/i.test(char)) { | |
charList.push(char); | |
keyList.push(i); | |
} | |
}); | |
// Replace sourceList String | |
keyList.map((key, i) => { | |
sourceList[key] = charList.reverse()[i]; | |
}); | |
// (a,(b,(c,(d,(e,(f)))))) | |
console.log(source); | |
// (f,(b,(d,(d,(b,(f)))))) | |
console.log(sourceList.join('')); | |
// @see https://www.v2ex.com/t/438930 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment