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 Beginners Course

Start learning how to code today with our free beginners course. Learn more.

Thank you for your message. It has been sent.
There was an error trying to send your message. Please try again later.