3

I m trying to replace some values from a date but it only change the first found value.

var date= cars.getAttribute("myLastDate");
var dateChanged= date.replace("/", ",");
alert (dateChanged);

Best regards.

Fran Rod
  • 586
  • 4
  • 14
  • 26
  • can you put your html code part? – polin Nov 27 '12 at 10:53
  • MDN is a really good reference on usage of JS functions like [`.replace()`](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/replace), and on JS in general. – nnnnnn Nov 27 '12 at 10:56

3 Answers3

4

If you don't need any regular expressions then I recommend the simpler split/join method to do search and replace.

var dateChanged = date.split("/").join(",");
Jim Blackler
  • 22,946
  • 12
  • 85
  • 101
3

You need to invoke the global flag using a regular expression:

var dateChanged= date.replace(/\//g, ",");
jAndy
  • 231,737
  • 57
  • 305
  • 359
1

replace in javascript use regular expression , u have to add /g at the end

date.replace(text/g,' ')
Mahmoud Farahat
  • 5,364
  • 4
  • 43
  • 59