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,620
  • 31
  • 96
  • 140

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
  • 666
  • 1
  • 11
  • 21
Josh Mein
  • 28,107
  • 15
  • 76
  • 87
0

Use

dateV= dateV.replace(/\//g,'-');
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
Gareth Parker
  • 5,012
  • 2
  • 18
  • 42