Created
January 7, 2018 03:09
-
-
Save westc/d9ecd094b6adf6edbb12a9fe01da69c8 to your computer and use it in GitHub Desktop.
A function which takes a string and returns a new one with only unique characters.
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
/** | |
* Gets a copy of the string with all duplicate characters removed. | |
* @name uniqChars | |
* @function | |
* @param {string} str | |
* Input string which will be duplicated and whose duplicate characters will | |
* be removed. | |
* @returns {string} | |
* A copy of `str` with all duplicate characters removed. | |
*/ | |
function uniqChars(str) { | |
return str.replace(/[\s\S](?=([\s\S]+))/g, function(c, s) { | |
return s.indexOf(c) + 1 ? '' : c; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment