Posted By

rumremix on 02/18/11


Tagged

javascript replace String a all in instances


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

wirenaught


javascript replace all instances in a string


 / Published in: JavaScript
 

URL: http://www.tizag.com/javascriptT/javascript-string-replace.php

Normal replace() function only replaces first instance.

  1. <script type="text/javascript">
  2. var visitorName = "Chuck";
  3. var myOldString = "Hello username! I hope you enjoy your stay username.";
  4. var myNewString = myOldString.replace(/username/g, visitorName);
  5.  
  6. document.write("Old string = " + myOldString);
  7. document.write("<br />New string = " + myNewString);
  8.  
  9. </script>

Report this snippet  

You need to login to post a comment.