Skip to content

Instantly share code, notes, and snippets.

@codebubb
Created November 25, 2015 15:07
Show Gist options
  • Select an option

  • Save codebubb/95b868bdc1f4fc9a23be to your computer and use it in GitHub Desktop.

Select an option

Save codebubb/95b868bdc1f4fc9a23be to your computer and use it in GitHub Desktop.
// Bonfire: Search and Replace
// Author: @codebubb
// Challenge: http://www.freecodecamp.com/challenges/bonfire-search-and-replace?solution=function%20myReplace(str%2C%20before%2C%20after)%20%7B%0A%09return%20str.replace(new%20RegExp(before)%2C%20(before.charCodeAt(0)%3E96%20%3F%20after%5B0%5D.toLowerCase()%20%3A%20after%5B0%5D.toUpperCase())%20%2B%20after.slice(1))%3B%0A%7D%0A
// Learn to Code at Free Code Camp (www.freecodecamp.com)
// Comments:
// Ahhh, one liners make me happy :)
function myReplace(str, before, after) {
return str.replace(new RegExp(before), (before.charCodeAt(0)>96 ? after[0].toLowerCase() : after[0].toUpperCase()) + after.slice(1));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment