replace() Method

By: Dusty Arlia
Published on Thursday, July 25, 2013, 09:39 PM
Last Updated on Tuesday, July 14, 2015 at 5:25 PM
Total Updates: 3

The replace() method searches a string for a specified regular expression and replaces that regular expression with a string. After the swap, a new string is returned. There are no changes to the original string. Here is the replace() method's basic syntax:


And here is a real example:

var original_string = "White Root Media";
var new_string = original_string.replace("Media", "Menu");

The location object has its own replace() method that is a little bit different. The replace() method for the location object replaces the current document with a new one. Here is the basic syntax:


And here is an example: