{
  "id": 19129404,
  "title": "You Filled the Email Template With replace and the Second {name} Went Out Unfilled",
  "url": "https://urgent.news/2026/11/09/you-filled-the-email-template-with-replace-and-the-second-name-went",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-11-09T09:42:11.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/sergueyasaelshinder/you-filled-the-email-template-with-replace-and-the-second-name-went-out-unfilled-4n3k"
  },
  "original_language": "en",
  "account": "The email template was straightforward, containing two spaces where a person's name should have appeared. At the beginning, it read \"Hi {name},\" and at the end, the message concluded with \"{name},\". The code responsible for inserting the names was simple, using the replace method to fill in the placeholders with the customer's first name. Initially, the test with a template having one {name} passed successfully. However, when the code was deployed to eleven thousand customers, every single one received an email with only the first name filled in, leaving the second space unfilled. The issue lies in JavaScript's replace function, which only replaces the first occurrence of the string, not all instances. This behavior, while documented, is not universally understood. To rectify this, two options were presented: utilizing replaceAll for JavaScript, which replaces every occurrence, or employing a regular expression with the 'g' flag. However, these solutions come with their own complexities. Additionally, the use of a replacement function allows for more control over the inserted text, preventing unintended alterations. Ultimately, the best approach would be to move away from using replace for this purpose and instead adopt a template engine that handles placeholders explicitly. Moreover, a pre-send check to ensure there are no remaining placeholders would further guarantee the integrity of the emails sent out.",
  "summary": "The template was simple. A renewal reminder, written by marketing, with two places for the name. Hi {name}, at the top. We will keep everything ready for you, {name}, near the bottom. The code that filled it was one line per field. body = body.replace(\"{name}\", customer.firstName); The test used a template with one {name} in it. It passed. Then the new copy went out to eleven thousand customers,…",
  "key_points": [
    "Template had two {name} placeholders.",
    "JavaScript replace function only replaced first {name}.",
    "Switching to replaceAll or regex fixed issue."
  ],
  "editors_take": null,
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}