You need to set the g flag to replace globally:

date.replace(new RegExp("/", "g"), '')
// or
date.replace(/\//g, '')

Otherwise only the first occurrence will be replaced.

Answer from Gumbo on Stack Overflow
Discussions

regex - JavaScript .replace only replaces first Match - Stack Overflow
You can play with it here, the default .replace() behavior is to replace only the first match, the /g modifier (global) tells it to replace all occurrences. More on stackoverflow.com
🌐 stackoverflow.com
How to replace the first occurrence of a character with a string in JavaScript? - Stack Overflow
I have a string which contains characters that should be replaced once (at first appearance). These characters are: L => will be replaced with the lecture name N => will be replaced with a name D => More on stackoverflow.com
🌐 stackoverflow.com
October 8, 2018
String.Replace only replaces first occurrence of matched string. How to replace *all* occurrences?
Coming from other programming languages, String.replace() typically replaces all occurrences of matching strings. However, that is not the case with javascript/typescript. I found a number of solut... More on stackoverflow.com
🌐 stackoverflow.com
best way to replace the first occurrence of an item in an array
Will it raise an error if the index is -1 and we try to replace something? 2017-01-18T22:44:10.68Z+00:00 ... See my edit. It won't raise error, but seems to have surprising behavior 2017-01-19T00:40:22.673Z+00:00 ... Not really! There are a few ways you could do it -- one is the way you have, another is using the search method rather than indexOf, which is more versatile in that it can accept regex arguments. Note however that search is not supported in some older JS ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Sabe
sabe.io › blog › javascript-replace-first-occurrence-character-in-string
How to Replace the First Occurrence of a Character in a String in JavaScript | Sabe
December 9, 2022 - The easiest way to replace the first occurrence of a character in a string is to use the replace() method.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › replace
String.prototype.replace() - JavaScript - MDN Web Docs
The replace() method of String ... or a RegExp, and the replacement can be a string or a function called for each match. If pattern is a string, only the first occurrence will be replaced....
🌐
freeCodeCamp
freecodecamp.org › news › javascript-replace-how-to-replace-a-string-or-substring-in-js
JavaScript Replace – How to Replace a String or Substring in JS
November 7, 2024 - In this example, the replace() method replaces the first occurrence of the word "JavaScript" with "Python" in the originalString variable.
🌐
Davidemanske
davidemanske.com › javascript-replace-only-replaces-first-occurence
JavaScript – Replace Only Replaces First Occurence
April 30, 2018 - JavaScript will only find the first instance of the target string and replace it. It will not continue to find other instances of the targeted string and replace it. Java/.Net/etc. will search for all occurrences of the target string and replace ...
🌐
W3Schools
w3schools.com › jsref › jsref_replace.asp
JavaScript String replace() Method
If you replace a value, only the first instance will be replaced.
🌐
Quora
quora.com › How-do-you-replace-all-occurrences-of-a-string-in-JavaScript-Using-str-replace-only-removes-the-first-occurrence-what-should-you-do-here
How to replace all occurrences of a string in JavaScript? Using str.replace() only removes the first occurrence, what should you do here - Quora
Answer (1 of 6): I don't know it it's good practice or not, but this is what I typically use, and it gets the job done: [code]var str = "This is the original string in its original form"; str = str.split("original").join("altered"); // str = "This is the altered string in its altered form"; [...
Find elsewhere
🌐
Flavio Copes
flaviocopes.com › home › javascript › the string replace() method
The String replace() method
February 11, 2019 - The replace() method finds the first occurrence of a string (or a regular expression match) inside a string, and returns a new string with that occurrence replaced.
🌐
W3Resource
w3resource.com › javascript-exercises › javascript-string-exercise-26.php
JavaScript validation with regular expression: Remove the first occurrence of a given 'search string' from a string - w3resource
July 17, 2025 - The above JavaScript code defines a function called "remove_first_occurrence()" that takes two parameters: 'str' (the main string) and 'searchstr' (the substring to be removed).
Top answer
1 of 2
4

Put your replacements in a map, once a replacement is made, set map[x] to x:

let lecture = "Math";
let name = "Ex01";
let date = "2018-10-05";

let repl = {
  'L': lecture,
  'N': name,
  'D': date
};

let file_string = "L_N_L_D"

let result = file_string.replace(/[LND]/g, x => {
  let r = repl[x];
  repl[x] = x;
  return r;
});

console.log(result)

Apart from solving the problem at hand, this also greatly simplifies your replacement function (think adding new placeholders, for example).

That being said, a real solution to your problem would be to follow the @marsze's advice and use unambiguous placeholders, like {...}, in which case the whole enterprise becomes simply

 repl = {...as before...}
 result = subject.replace(/{(.+?)}/g, (_, x) => repl[x])
2 of 2
4

Answering your general question "replace only the first occurrence of a character", you could do it like this:

var lecture = "Math";
var name = "Ex01";
var date = "2018-10-05";
var found = {};
var file_string = "L_N_L_D";
var filename_result = file_string.split("").map(function (character) {
  if (!found[character]) {
    found[character] = true;
    switch (character) {
      case "L": return lecture;
      case "N": return name;
      case "D": return date;
    }
  }
  return character;
}).join("");
console.log(filename_result);

You should probably explain where that odd format of the file_string comes from. Are there other similar use cases? Or is this just about this specific example? Understanding the original requirements would help a lot.

If the file_string is supposed to be a configurable format string, then it should be improved. A sequence which serves as a placeholder to be replaced should (or must) be different from a literal, e.g.:

var filename_format = "{L}_{N}_L_{D}";
🌐
CoreUI
coreui.io › blog › how-to-replace-all-occurrences-of-a-string-in-javascript
How to replace all occurrences of a string in JavaScript? · CoreUI
August 31, 2024 - However, by default, this method searches for the pattern and replaces only the first occurrence of it: const str = 'hello world, hello universe' const newStr = str.replace('hello', 'hi') console.log(newStr) // 'hi world, hello universe' In ...
🌐
Designcise
designcise.com › web › tutorial › how-to-replace-all-occurrences-of-a-word-in-a-javascript-string
How to Replace All Occurrences of a Word in a JavaScript String? - Designcise
January 29, 2021 - If that is a concern for you, then you may use String.prototype.replace() instead (as it works in the same way and has great browser support). You must remember though, that by default replace() only replaces the first occurrence of a word.
🌐
Mimo
mimo.org › glossary › javascript › replace
JavaScript Replace Method: Advanced String Manipulation
.replace(): By default, it replaces only the first occurrence of a substring.
🌐
Stack Abuse
stackabuse.com › replace-occurrences-of-a-substring-in-string-with-javascript
Replace Occurrences of a Substring in String with JavaScript
September 25, 2020 - ... The grey-haired husky has one blue and one brown eye. We'll want to replace the word "blue" with "hazel". The simplest way to do this is to use the built-in replace() function. It accepts a regular expression as the first argument, and the word(s) we're replacing the old ones with as the ...