Skip to content

Instantly share code, notes, and snippets.

@doublejosh
Created September 28, 2016 00:12
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 doublejosh/437353bef204aa42a943f9059a83f262 to your computer and use it in GitHub Desktop.
Save doublejosh/437353bef204aa42a943f9059a83f262 to your computer and use it in GitHub Desktop.
Remove specific prefix word from camelCase strings
/**
* Remove prefix word from camelCase strings.
*
* @example stripCamelPrefix('myOldThing'); // oldThing
*
* @param {string} str
* @param {string} prefix
*
* @return {string}
*/
function stripCamelPrefix (str, prefix) {
var words = str.split(/(?=[A-Z])/);
// Found and more words.
if (words.length > 0 && words[0] === prefix) {
// Remove prefix (destructive), adjust new first word.
words.shift();
words[0] = words[0].toLowerCase();
// Create new string.
return words.join('');
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment