javascript replace

Javascript Replace Method

I've noticed that I always forget the syntax when needing to do a javascript replace, so I decided to write a quick post about it.

  var stringToModify = "This is the string I want to modify";
  var newString = stringToModify.replace("modify", "change");
  alert(newString);

the newString variable now holds the String value “This is the string I want to change”

The javascript replace () method takes two parameters:

1) Search Value (required)
2) Replace Value (required)

It will return a new String var.

Quick and dirty example, hopefully now I won't need to look this up anymore. Besides, the best way to remember something is to teach something right? So I hope this post helps you out :)

Free Java Roadmap

Discover exactly what you need to learn and where to start in order to become a professional coder.