Ok. I don't understand

Tell us what’s happening:
Describe your issue in detail here.
I know I go overboard with the thinking part of programming but this particular challenge confuses me. I’m suppose to replace a string with another string maintaining the cases of the replaced string. In my mind, this should work no matter where the case change or capitalization is. I tried using a regular expression syntax but I couldn’t get it done (maybe I don’t know how to) but seeing the solutions given confuses me more. It focused on the first character in the string and I asked myself, what if the capitalization is in the second or third character, well I tested it and the output was wrong. As much as it seems like I’m deviating from the main question, I would like to know how to answer this question when the capitalization is in any position.

  **Your code so far**

function myReplace(str, before, after) {
if (/^[A-Z]/.test(before)) {
  after = after[0].toUpperCase() + after.substring(1)
} else {
  after = after[0].toLowerCase() + after.substring(1)
}
return str.replace(before, after);;
}

console.log(myReplace("A quick brown fox jumped over the lazy dog", "jumped", "leaped"));
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36

Challenge: Search and Replace

Link to the challenge:

first solution that comes to mind is loop throught the before word and match the case size for every letter on the after word. I find it counter-intuitive however to match all cases of the two words, as they can be of different length and if you read closely, you will notice the challenge requires you to match only the first letter
Let me know if you have troubles rendereding the suggested approach into code

2 Likes

Hi @nwobodoemmanuel479 ,

If we need to check for any upper case letter of ‘before’ and change it accordingly in the ‘after’, we could use the match method.

upperCaseArr = [].concat(before.match(/[A-Z]/g));

This would give us an array of capital letters in ‘before’.
We could use a for loop to check the index of each capital letter and accordingly capitalise the letter at that index in ‘after’ (provided that the index exists in ‘after’).

Hope this helps.

1 Like

this approach could run into unwanted behavior if the same capital letter is present twice and you use simple indexOf to check their index(it will always return the first matched index), so you will also need to create a more advanced indexing method

1 Like

Yeah, that’s true.
Found another string method which would be better suited- matchAll()
This method also gives us the index along with the matching regex for all the matches.

upperCaseArr = [...before.matchAll(/[A-Z]/g)]

Sharing the link below:

1 Like

Yes please, thank you.

Thank you very much.

function myReplace(str, before, after) {
  after=after.split('')
  for (const index in before){
    if (after[index]) { 
      if (before[index]===before[index].toUpperCase()) {
        after[index]=after[index].toUpperCase() 
        continue  
      }
      after[index]=after[index].toLowerCase()
    }
  }
  return str.replace(before, after.join('')); 
}

Since i might need to alter the casing of the “after” word, i convert it to array, as its easier to change their content. I used for in loop as it matches my needs, altho regular for loop can do too. In the loop, i make sure a letter, on the current after[index] exists, in case “before” word is longer. Then if the “before” letter is upper case, i make sure the “after” letter is too, otherwise i ensure its lower case. Instead of continue, you can use else block for the lower case action. In the end, make sure to put after back as a string

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.