0

I have a string field 01/01/1986 and I am using replace method to replace all occurrence of / with -

var test= '01/01/1986';
test.replace('//g','-')

but it does't give desire result. Any pointer would be helpful.

Bahman Parsa Manesh
  • 2,314
  • 3
  • 16
  • 32
Vini
  • 29
  • 7

2 Answers2

7

You just have a couple issues: don't put the regex in quotes. That turns it into a string instead of a regex and looks for that literal string. Then use \/ to escape the /:

var test= '01/01/1986';
console.log(test.replace(/\//g,'-'))
Mark
  • 90,562
  • 7
  • 108
  • 148
0

A quick way is to use split and join.

var test= '01/01/1986';
var result = test.split('/').join('-');

console.log(result);

Note too that you need to save the result. The original string itself will never be modified.