-1

Write a function normalize, that replaces '-' with '/' in a date string.

Example: normalize('20-05-2017') should return '20/05/2017'.

This is what I wrote:

function normalize(str) {
    let replaced = str.replace('-', '/')
    return replaced
}

I can't replace the other - with / can someone explain how to do this?

enzo
  • 9,861
  • 3
  • 15
  • 38
Zack Hem
  • 1
  • 1

2 Answers2

1

When you use replace, only the first instance is replaced. See replace

What you can do is either

Use replaceAll

const replaced = str.replaceAll('-', '/');

Or

const replaced = str.replace(/-/g, '/');

The g means Global, and causes the replace call to replace all matches, not just the first one

Abito Prakash
  • 4,368
  • 2
  • 13
  • 26
0

Instead of str.replace('-','/') use str.replaceAll('-','/')

STA
  • 30,729
  • 8
  • 45
  • 59
simu XD
  • 34
  • 4