Get Programming with JavaScript - Listing 19.02
Listing 19.02 - Replacing one string with another
JS Bin
var before = "<h3>{{title}}</h3>";
var after = before.replace("{{title}}", "Fitness App v1.0 Live!");
console.log(before);
console.log(after);
Further Adventures
Listing 19.02 - Replacing one string with another - Tasks 2 & 3
- Declare a title variable and assign it a value.
- Use before.replace to replace the {{title}} placeholder with the value of the title variable, rather than with a string literal.
var before = "<h3>{{title}}</h3>";
var title = "Fitness App v1.0 Live!";
var after = before.replace("{{title}}", title);
console.log(before);
console.log(after);
Listing 19.02 - Replacing one string with another - Tasks 4 & 5
- Include a second placeholder in the original string.
- Use the replace method to fill both your placeholder and {{title}} with a value.
var before = "<h3>{{title}}</h3><p>{{message}}</p>";
var title = "Fitness App v1.0 Live!";
var msg = "Finally, the app is available...";
var after = before.replace("{{title}}", title);
after = after.replace("{{message}}", msg);
console.log(before);
console.log(after);