Posted By

fengelz on 07/26/10


Tagged

javascript replace


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

wirenaught


Javascript string replace (all occurrences)


 / Published in: JavaScript
 

  1. //replace all whitespaces with underscore
  2. string.replace(new RegExp(/\s/g), '_');
  3.  
  4. //replace the word 'replace' with 'replaced'
  5. string.replace(/replace/g, 'replaced');

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: fengelz on December 5, 2010

I always forget how this goes. The standard replace function only replaces the first instance in a string. But this is how you replace all occuring instances...

You need to login to post a comment.