admin管理员组

文章数量:1290267

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.

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.

Share Improve this question asked Nov 27, 2012 at 10:52 Fran RodFran Rod 5964 gold badges14 silver badges26 bronze badges 2
  • can you put your html code part? – polin Commented Nov 27, 2012 at 10:53
  • MDN is a really good reference on usage of JS functions like .replace(), and on JS in general. – nnnnnn Commented Nov 27, 2012 at 10:56
Add a ment  | 

3 Answers 3

Reset to default 4

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

var dateChanged = date.split("/").join(",");

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

var dateChanged= date.replace(/\//g, ",");

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

date.replace(text/g,' ')

本文标签: javascriptdate and replace method only change the first found valueStack Overflow