Posted By

fengelz on 07/26/10


javascript replace

Versions (?)

Who likes this?

1 person have marked this snippet as a favorite


Javascript string replace (all occurrences)

 / Published in: JavaScript

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

Report this snippet  


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.