0

I need to replace hypen from string using Javascript but as per my code its not working.

function replace(){
     var str='20-03-2019';
     str.replace(/-/g,'/');
     console.log(str);
}
          
replace();

Here I need to replace hypen(-) with / and output should like 20/03/2019.

Liam
  • 27,717
  • 28
  • 128
  • 190
subhra
  • 173
  • 5

1 Answers1

1

String is immutable, so you need to do :

function replace(){
     var str='20-03-2019';
     str = str.replace(/-/g,'/');
     alert('date ' + str);
}
          
replace();
Vinz243
  • 9,654
  • 10
  • 42
  • 86