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") ?

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay