Deadpool
Deadpool

Reputation: 8240

Global string replacement of "/" to "-" in date format string

I have a string which is 26/12/1999. Now i want to convert it to 26-12-1999 but I am getting problems. Using simple '/' by '-' not working. //g is taking itself as comments. Using /g is not fetching desired results. Pls help.

<html>
<head></head>
<body> 
<script> 
var a = "26/12/1987";
var b = a.replace("/","-");
console.log(b);
</script> 
</body> 
</html>

Please Help!

Upvotes: 0

Views: 50

Answers (1)

nicael
nicael

Reputation: 18995

Using /g is not fetching desired results.

It is, you are probably just using it improperly. You should pass the regex, not string.

var a = "26/12/1987";
var b = a.replace(/\//g,"-");
console.log(b);

Upvotes: 3

Related Questions