freeCodeCamp/guide/english/certifications/javascript-algorithms-and-d.../intermediate-algorithm-scri.../search-and-replace/index.md

12 KiB

title
Search and Replace

:triangular_flag_on_post: Remember to use Read-Search-Ask if you get stuck. Try to pair program :busts_in_silhouette: and write your own code :pencil:

:checkered_flag: Problem Explanation:

You will create a program that takes a sentence, then search for a word in it and replaces it for a new one while preserving the uppercase if there is one.

:speech_balloon: Hint: 1

  • Find the index where before is in the string.

try to solve the problem now

:speech_balloon: Hint: 2

  • Check first letter case.

try to solve the problem now

:speech_balloon: Hint: 3

  • Strings are immutable, you will need to save the edits on another variable, even if you must reuse the same one just to make it look like the changes where done using just that one variable.

try to solve the problem now

Spoiler Alert!

warning sign

Solution ahead!

:beginner: Basic Code Solution:

function myReplace(str, before, after) {
  // Find index where before is on string
  var index = str.indexOf(before);
  // Check to see if the first letter is uppercase or not
  if (str[index] === str[index].toUpperCase()) {
    // Change the after word to be capitalized before we use it.
    after = after.charAt(0).toUpperCase() + after.slice(1);
  }
  // Now replace the original str with the edited one.
  str = str.replace(before, after);

  return str;
}

// test here
myReplace("A quick brown fox jumped over the lazy dog", "jumped", "leaped");

:rocket: Run Code

Code Explanation:

  • Use indexOf() to find location of before in string.
  • If first letter of before is capitalized, change first letter of after to uppercase.
  • Replace before in the string with after.
  • Return the new string.

:sunflower: Intermediate Code Solution:

function myReplace(str, before, after) {
//Create a regular expression object
  var re = new RegExp(before,"gi");
//Check whether the first letter is uppercase or not
  if(/[A-Z]/.test(before[0])){
  //Change the word to be capitalized
    after = after.charAt(0).toUpperCase()+after.slice(1);
     }
     //Replace the original word with new one
  var  newStr =  str.replace(re,after);

 return newStr;
}

// test here
myReplace("A quick brown fox jumped over the lazy dog", "jumped", "leaped");

:rocket: Run Code

Code Explanation:

  • In this solution, regular expression [A-Z] is used to check if character is uppercase.
  • Create a new regular expression object, re.
  • If first letter of before is capitalized, change the first letter of after to uppercase.
  • Replace before with after in the string.
  • Return the new string.
  • JS Regex Resources

:rotating_light: Advanced Code Solution:

function myReplace(str, before, after) {

    // create a function that will change the casing of any number of letter in parameter "target"
    // matching parameter "source"
    function applyCasing(source, target) {
        // split the source and target strings to array of letters
        var targetArr = target.split("");
        var sourceArr = source.split("");
        // iterate through all the items of sourceArr and targetArr arrays till loop hits the end of shortest array
        for (var i = 0; i < Math.min(targetArr.length, sourceArr.length); i++){
            // find out the casing of every letter from sourceArr using regular expression
            // if sourceArr[i] is upper case then convert targetArr[i] to upper case
            if (/[A-Z]/.test(sourceArr[i])) {
                targetArr[i] = targetArr[i].toUpperCase();
            }
            // if sourceArr[i] is not upper case then convert targetArr[i] to lower case
            else targetArr[i] = targetArr[i].toLowerCase();
        }
        // join modified targetArr to string and return
        return (targetArr.join(""));
    }

    // replace "before" with "after" with "before"-casing
    return str.replace(before, applyCasing(before, after));
}

// test here
myReplace("A quick brown fox jumped over the lazy dog", "jumped", "leaped");

:rocket: Run Code

Code Explanation:

  • Both the before and after are passed as arguments to applyCasing().
  • The function applyCasing() is used to change the case of respective characters in targetArr i.e., after in accordance with that of characters in sourceArr i.e., before.
  • replace() is used to replace before with after, whose casing is same as before.

:rotating_light: Advanced Code Solution Alternative:

// Add new method to the String object, not overriding it if one exists already
String.prototype.capitalize =  String.prototype.capitalize ||
    function() {
        return this[0].toUpperCase() + this.slice(1);
    };

const Util = (function () {
// Create utility module to hold helper functions
    function textCase(str, tCase) {
        // Depending if the tCase argument is passed we either set the case of the
        // given string or we get it.
        // Those functions can be expanded for other text cases.
        
        if(tCase) {
            return setCase(str, tCase);
        } else {
            return getCase(str);
        }

        function setCase(str, tCase) {
            switch(tCase) {
                case "uppercase": return str.toUpperCase();
                case "lowercase": return str.toLowerCase();
                case "capitalized": return str.capitalize();
                default: return str;
            }
        }

        function getCase(str) {
            if (str === str.toUpperCase()) { return "uppercase"; }
            if (str === str.toLowerCase()) { return "lowercase"; }
            if (str === str.capitalize()) { return "capitalized"; }
            return "normal";
        }
    }

    return {
        textCase
    };
})();

function myReplace(str, before, after) {
    const { textCase } = Util;
    const regex = new RegExp(before, 'gi');
    const replacingStr = textCase(after, textCase(before));

    return str.replace(regex, replacingStr);
}

:rocket: Run Code

:rotating_light: Advanced Code Solution Alternative 2:


function myReplace(str, before, after) {
  const myArr = str.split(' ');
  const [wordToReplace] = myArr.filter(item => item === before);
  return  wordToReplace[0].toUpperCase() !== wordToReplace[0]
  ? myArr.map(item => item === before ? after : item).join(' ')
  : myArr.map(item => item === before? after[0].toUpperCase() + after.slice(1) : item).join(' ');
}

// test:
myReplace("A quick brown fox jumped over the lazy dog", "jumped", "leaped");

:clipboard: NOTES FOR CONTRIBUTIONS:

  • :warning: DO NOT add solutions that are similar to any existing solutions. If you think it is similar but better, then try to merge (or replace) the existing similar solution.
  • Add an explanation of your solution.
  • Categorize the solution in one of the following categories — Basic, Intermediate and Advanced. :traffic_light:
  • Please add your username only if you have added any relevant main contents. (:warning: DO NOT remove any existing usernames)

See :point_right: Wiki Challenge Solution Template for reference.