Replacing a Word in a String

Another common String object task is replacing one substring with another. To replace a word or phrase in a string, you use the replace() method. The following code replaces the text "<username>" with the value of the variable username:

var username = "Brad";var output = "<username> please enter your password: ";output.replace("<username>", username);

Get Learning AngularJS now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.