Simple solution for search and replace challenge

After this fun post and one-liner challenge, I thought it’d be fun to try the same with this one, improving on what I had originally used as my solution:

function myReplace(str, before, after) {
  return str.replace(before,
    before.charAt(0) === before.charAt(0).toUpperCase()
      ? after.charAt(0).toUpperCase() + after.slice(1)
      : after
  );
}

The trouble with these one-liners though is that while fun, they’re often not as readable. I like Crockford’s suggestion to split the ternary operator into multiple lines.

Also, this also isn’t very robust, and only considers the first character for preserving case (as the challenge requires). Working tech-support for over two years has pushed me to value considering the edge-case. Might be fun to try to see how this could be pushed to preserve case for the entirety of the string.