DEV Community

Cover image for Replace a Substring With Replace Method
Calvin
Calvin

Posted on

Replace a Substring With Replace Method

The replace method returns a new string with the new substring you add as a parameter included in place of the other substring that you add as a parameter to be replaced.

Here is an example of the method being used:


var poem = "She sells seashells by the seashore. The shells she sells are surely seashells."

// replace all the sea with ocean

poem = poem.replace("sea", "ocean")

poem = poem.replace("sea", "ocean")

poem = poem.replace("sea", "ocean")

console.log(poem);
// -> She sells oceanshells by the oceanshore. The shells she sells are surely oceanshells.


Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
page_source profile image
Satyam Mishra

why not just do poem.replaceAll("sea", "ocean") ?