-1

I know this is a simple task but I can't seem to find the solution.

I've got a date in the format: 20/02/2013 I just wanna replace the / by - I've got that so far but it only replaces the first slash... Don't know why not the second:

date = 20/02/2013;
date.replace('/', '-');

Thanks for any help.

Got The Fever Media
  • 750
  • 1
  • 9
  • 27
  • 1
    possible duplicate of [Fastest method to replace all instances of a character in a string](http://stackoverflow.com/questions/2116558/fastest-method-to-replace-all-instances-of-a-character-in-a-string) – Matt Feb 18 '13 at 12:44

2 Answers2

5

You need a regular expression with global flag:

"20/02/2013".replace(/\//g, "-");   // "20-02-2013"

Another way is to use split/join:

"20/02/2013".split("/").join("-");  // "20-02-2013"
VisioN
  • 143,310
  • 32
  • 282
  • 281
0

Use the global flag.

str.replace(/\//g, '-');
Roel
  • 3,089
  • 2
  • 30
  • 34