1The general syntax for this method is:
2
3stringName.replace(searchChar, replacementChar);
4/*Given a search string searchChar and a replacement value
5replacementChar, this method returns a copy of stringName with the
6first occurrence of searchChar replaced by replacementChar.*/
7
8//Example
9"carrot".replace("r", "t");
10
11"Launch Code".replace(" ", "");
12//catrot
13//LaunchCode
14
15//Example
16/*Some email providers, including Gmail, allow users to put a .
17anywhere before the @ symbol. This means that
18fake.email@launchcode.org is thesame as fakeemail@launchcode.org.*/
19
20//Remove the . before the @ symbol in an email address.
21
22let input = "fake.email@launchcode.org";
23let email = input.replace(".", "");
24console.log(email);
25//fakeemail@launchcode.org
26
27/*This example illustrates a common use case of replace, which is
28to remove a character by replacing it with the empty string.*/
29
30/*Notice in the last example that if there is not a . before the
31 @ symbol, the . that is part of the domain, launchcode.org would
32 be inadvertently removed. In a real application, we would want to
33 isolate the portion in front of @ using slice.*/
34