3

Possible Duplicate:
Fastest method to replace all instances of a character in a string = Javascript
How to replace all points in a string in JavaScript

I have a string 2012/04/13. I need to replace the / with a -. How can i do this ?

var dateV = '2012/04/13';
dateV= dateV.replace('/','-');

It only replaces the first / and not all / in the string (2012-04/13). What should i do to correct this ?

Community
  • 1
  • 1
Sharon Watinsan
  • 9,358
  • 30
  • 93
  • 131

2 Answers2

4

You need to do a global regex replace using the global regex option. This should work for you:

var dateV = '2012/04/13';
var regex = new RegExp("/", "g"); // "g" - global option
dateV = dateV.replace(regex, "-");
console.log(dateV);
mitesh7172
  • 606
  • 10
  • 20
Josh Mein
  • 27,292
  • 12
  • 73
  • 84
0

Use

dateV= dateV.replace(/\//g,'-');
gen_Eric
  • 214,658
  • 40
  • 293
  • 332
Gareth Parker
  • 4,912
  • 2
  • 17
  • 42