-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,121
  • 2
  • 12
  • 36
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
  • 2,669
  • 2
  • 9
  • 20
0

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

sta
  • 24,192
  • 8
  • 39
  • 53
simu XD
  • 34
  • 4