≡ Menu

Javascript Replace

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 🙂

{ 4 comments… add one }
  • nick August 9, 2012, 5:46 am

    Was this post out of sequence? I went from hello world 2 to JavaScript replace. Or was this more of a sub-post to help with your memory?

    Is this post about JavaScript? or is it just a bad name?

    Thank’s for the tutorials Trevor, I’m really enjoying them mate.

    • Trevor Page August 9, 2012, 6:29 am

      It was a quick post moreso for myself, it is JavaScript and not Java. I’ll eventually expand my content to include all the aspects of web development (which will include Javascript), so in the future this little one-off post won’t feel so out of place 😉

      And thank YOU Nick for the comment and kind words,

      all the best

  • Durim February 3, 2013, 12:14 pm

    Thanks Trevor!

    I really enjoy your tutorials, you are so clear, you explain things very well!

    • Trevor Page February 5, 2013, 10:27 am

      Hey Durim,

      Always great to hear from you, thanks for your kind words and I look forward to hearing from you in the future!

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.