Skip to content

Instantly share code, notes, and snippets.

@westc
Created January 7, 2018 03:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save westc/d9ecd094b6adf6edbb12a9fe01da69c8 to your computer and use it in GitHub Desktop.
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.
/**
* 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