JavaScript examples: text and names

Copy-ready Run Custom Code snippets for names, salutations, slugs, masking, templates and cleaning up text, using plain JavaScript.

  • Verified Nov 17, 2026
  • Classic builder

On this page
  1. Before you start
  2. Build a full name and initials
  3. Build a formal salutation
  4. Title-case text
  5. Build a URL slug
  6. Shorten text with an ellipsis
  7. Mask all but the last four digits
  8. Fill {{placeholders}} in a template
  9. Get the domain of an email address
  10. Escape a value for a CSV cell
  11. Turn HTML into plain text
  12. Pad a number to a fixed width
  13. Count words and estimate reading time
  14. Flag text that contains certain words
  15. Remove emoji and other non-ASCII characters

Snippets for a Run Custom Code step that build and clean up text: full names, salutations, slugs, masked numbers, filled-in templates and plain text from HTML. Each one returns its results with returnData so later steps can use them. For how the step works, see Run custom JavaScript in a workflow.

Before you start #

  1. Add the step Add a Run Custom Code step to your workflow and leave Language set to JavaScript.
  2. Add the inputs Under Data to use in the code, click + Add Input once for each input the snippet lists. Type the name from the snippet in Variable name and choose where the value comes from, for example Record Value (Source) or Custom Value.
  3. Paste and test Click the Code box to open the code editor, paste the snippet, and click Run. The Response tab shows what the code returned.
  4. Use the results In a later step, open the value picker, choose Step Data, pick this step, and pick the key the snippet returned (for example full_name).

An input is available in the code as $ followed by its variable name: an input named first_name is $first_name. Add every input a snippet uses; a name with no matching input stops the code with a "not defined" error.

Build a full name and initials #

Joins the parts that are not blank and falls back to "Unnamed".

JavaScript
// Inputs: first_name, last_name
const first = String($first_name ?? "").trim();
const last = String($last_name ?? "").trim();

returnData("full_name", [first, last].filter(Boolean).join(" ") || "Unnamed");
returnData("initials", (first.charAt(0) + last.charAt(0)).toUpperCase());

Build a formal salutation #

Returns "Dear Dr. Smith," or "Dear Smith," and falls back to "Hello," when there is no last name.

JavaScript
// Inputs: title ("Mr.", "Ms.", "Dr."), last_name
const title = String($title ?? "").trim();
const last = String($last_name ?? "").trim();

let salutation = "Hello,";
if (title && last) salutation = `Dear ${title} ${last},`;
else if (last) salutation = `Dear ${last},`;

returnData("salutation", salutation);

Title-case text #

JavaScript
// Input: text   "the QUICK brown fox" -> "The Quick Brown Fox"
const titled = String($text ?? "")
  .toLowerCase()
  .split(/\s+/)
  .filter(Boolean)
  .map(w => w.charAt(0).toUpperCase() + w.slice(1))
  .join(" ");

returnData("title", titled);

Build a URL slug #

Keeps lowercase letters and digits and turns everything else into single dashes. Letters with accents are removed, not converted.

JavaScript
// Input: title   "Hello World!! v2" -> "hello-world-v2"
const slug = String($title ?? "")
  .toLowerCase()
  .replace(/[^a-z0-9]+/g, "-")
  .replace(/^-+|-+$/g, "");

returnData("slug", slug || "untitled");

Shorten text with an ellipsis #

JavaScript
// Inputs: body, max_chars (Custom Value, for example 140)
const text = String($body ?? "").trim();
const max = Number($max_chars) || 140;

const preview = text.length > max
  ? text.slice(0, max - 1).trimEnd() + "…"
  : text;

returnData("preview", preview);

Mask all but the last four digits #

JavaScript
// Input: number   "4242424242424242" -> "************4242"
const digits = String($number ?? "").replace(/\D/g, "");
const last4 = digits.slice(-4);

returnData("masked", digits.length > 4 ? "*".repeat(digits.length - 4) + last4 : digits);
returnData("last4", last4);

Fill {{placeholders}} in a template #

getAppDetails() returns the app’s id, name and timezone. Placeholders with no value become blank.

JavaScript
// Inputs: template ("Hi {{name}}, welcome to {{app}}"), name
const values = {
  name: $name || "there",
  app: getAppDetails().name || ""
};

const rendered = String($template ?? "").replace(/\{\{\s*(\w+)\s*\}\}/g, (match, key) =>
  key in values ? String(values[key]) : ""
);

returnData("rendered", rendered);

Get the domain of an email address #

JavaScript
// Input: email   "ada@acme.co.uk" -> "acme.co.uk"
const email = String($email ?? "").trim().toLowerCase();
const domain = email.includes("@") ? email.split("@").pop() : "";

returnData("domain", domain);
returnData("is_gmail", domain === "gmail.com");

Escape a value for a CSV cell #

Wraps the value in quotes when it contains a comma, a quote or a line break, and doubles any quotes inside it.

JavaScript
// Input: value
const s = String($value ?? "");
const cell = /[",\r\n]/.test(s) ? `"${s.replace(/"/g, '""')}"` : s;

returnData("csv_cell", cell);

Turn HTML into plain text #

Good enough for previews and character counts. It is not a security filter.

JavaScript
// Input: html   "<p>Hi <b>there</b>!</p>" -> "Hi there!"
const plain = String($html ?? "")
  .replace(/<br\s*\/?>/gi, "\n")
  .replace(/<[^>]+>/g, "")
  .replace(/&nbsp;/g, " ")
  .replace(/&amp;/g, "&")
  .replace(/[ \t]+\n/g, "\n")
  .trim();

returnData("plain_text", plain);
returnData("char_count", plain.length);

Pad a number to a fixed width #

JavaScript
// Input: invoice_number   42 -> "INV-000042"
const n = parseInt($invoice_number, 10) || 0;

returnData("invoice_code", "INV-" + String(n).padStart(6, "0"));

Count words and estimate reading time #

Assumes 200 words a minute.

JavaScript
// Input: body
const text = String($body ?? "").trim();
const words = text ? text.split(/\s+/).length : 0;

returnData("word_count", words);
returnData("reading_minutes", Math.max(1, Math.ceil(words / 200)));

Flag text that contains certain words #

Change the terms list to the words you care about. To branch on the result, see JavaScript examples: return a status to branch on.

JavaScript
// Input: text
const haystack = String($text ?? "").toLowerCase();
const terms = ["urgent", "asap", "immediately", "refund"];

const hits = terms.filter(term => haystack.includes(term));

returnData("flagged", hits.length > 0);
returnData("flagged_terms", hits.join(", "));

Remove emoji and other non-ASCII characters #

JavaScript
// Input: name   Removes emoji and every character outside plain ASCII,
// including accented letters ("José" -> "Jos"). Use it only where that is wanted.
const cleaned = String($name ?? "")
  .replace(/[^\x20-\x7E]/g, "")
  .replace(/\s+/g, " ")
  .trim();

returnData("clean_name", cleaned);

Was this page helpful?

Something wrong or missing on this page? Tell us.