Why does javascript replace only first instance when using replace? [duplicate]

You need to set the g flag to replace globally:

date.replace(new RegExp("https://stackoverflow.com/", "g"), '')
// or
date.replace(/\//g, '')

Otherwise only the first occurrence will be replaced.

Leave a Comment