Skip to content

Instantly share code, notes, and snippets.

@minsooshin
Last active November 22, 2015 04:01
Show Gist options
  • Save minsooshin/13e0b293561de7920aed to your computer and use it in GitHub Desktop.
Save minsooshin/13e0b293561de7920aed to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/minsooshin 's solution for Bonfire: Search and Replace
// Bonfire: Search and Replace
// Author: @minsooshin
// Challenge: http://www.freecodecamp.com/challenges/bonfire-search-and-replace
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function myReplace(str, before, after) {
if ((/^[A-Z]/).test(before)) after = after.charAt(0).toUpperCase() + after.slice(1);
str = str.replace(new RegExp(before, 'gi'), after);
return str;
}
myReplace("He is Sleeping on the couch", "Sleeping", "sitting");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment