CSE 154 Examples

Replacing a String

Description :

In Javascript, you can replace portions of a string by using the replace() method.
The replace() method replaces a specified string with another string.
This method doesn't change the original string. Instead it returns a NEW string.
By Default, only the FIRST Occurence is replaced. In addition, the strings are case sensitive.

Syntax : let changed_string = original_string.replace(string_to_be_replaces, string_to_replace_with);

Examples :

				 
	         let str = "Cats are the best!";
	         let replaced = str.replace("Cats", "Dogs");
				 
	         replaced =  ?? 
       
				 
	         let str = "Cats are Cats"
	         let replaced = str.replace("Cats", "Dogs"); //only the first occurence is replaced
				 
	         replaced =  ?? 
       
				 
	         let str = "Cats";
	         let notReplaced = str.replace("cats", "Dogs"); //the replace function is case sensitive "cats" != "Cats"
				 
	         notReplaced =  ?? 
				 
	         let str = "Cats";
	         let replaced = str.replace("Cats", "Dogs");
				 
	         replaced =  ??