Don't understand string replace method

Hello,

I was working on an exercise in Javascript on Fixing capitalization. And I still don’t quite understand how the replace method works.
It is asked in this exercise to fix capitalization on cities names.

// I must fix the capitalization on : liVERpoOL
var name = “liVERpoOL”;
name = name.toLowerCase();
var firstLetter = name.slice(0,1); // ‘l’
name = name.replace(firstLetter, firstLetter.toUpperCase());

// name = Liverpool

But, there are two letters ‘L’ in the name “Liverpool”, why is the ‘L’ at the end is not replaced as well.

Thank you : )

Hi there @FloppyJunior.

This is a good question. The reason is stated in the first paragraph of the replace() reference page: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace

Basically, you can give the method the string to replace as an exact string, like we’ve done in the example you are referring to here, or a regular expression. In the first case, only the first instance of the string that is found is replaced. To replace them all, you’d need to use a regular expression to specify the string pattern to match.

Thank you @chrisdavidmills ! Next time I will read the documentation : )
Have a good day !

@FloppyJunior you are very welcome. There is so much to learn and remember; we all miss details sometimes.

:bomb: Spoiler Alert :bomb: :bomb: Spoiler Alert :bomb:
I’m posting this to help other MDN students who might be working through this course behind me. So, don’t read this unless your truly stuck (such as I was :thinking: ).

If so, you can go to my project version of this learning exercise,
Fixing Capitalization to see my notes and examples for this exercise.