How to Split a String with Multiple Separators in TypeScript

You split a string with multiple separators in TypeScript by passing a regular expression to split(). Put single-character separators in a character class like /[,;|]/, and you’ll get back a string[] with one piece per value:

const parts = "Austin,Dallas;Houston|El Paso".split(/[,;|]/);
// ["Austin", "Dallas", "Houston", "El Paso"]

All examples were compiled in strict mode with TypeScript 7.0.2 and Node.js v22.22.2, and I pasted the console output exactly as it printed.

Split a string by multiple single-character separators

A character class matches any one character inside the brackets, so it splits on a comma, semicolon or pipe at once:

const cities: string = "Austin,Dallas;Houston|El Paso";

const parts: string[] = cities.split(/[,;|]/);
console.log(parts);

const messy: string = "Austin , Dallas;  Houston |El Paso";
console.log(messy.split(/[,;|]/));          // spaces stay attached
console.log(messy.split(/\s*[,;|]\s*/));    // spaces eaten with the separator

Output:

[ 'Austin', 'Dallas', 'Houston', 'El Paso' ]
[ 'Austin ', ' Dallas', '  Houston ', 'El Paso' ]
[ 'Austin', 'Dallas', 'Houston', 'El Paso' ]
Command Prompt output of TypeScript split with a character class on commas, semicolons and pipes, with and without surrounding spaces
The plain class keeps stray spaces; adding \s* on both sides removes them.

Wrapping the class in \s* saves a trim() on every piece. If you only need to turn a string into an array with one delimiter, my guide to converting a string to an array in TypeScript covers the simpler cases.

Why does split() return empty strings?

Two separators in a row leave nothing between them, so split() returns an empty string there. Leading and trailing separators do the same:

const raw: string = ",Denver,,Boulder;;Aurora,";

console.log("plain:  ", raw.split(/[,;]/));
console.log("with +: ", raw.split(/[,;]+/));
console.log("filter: ", raw.split(/[,;]+/).filter(Boolean));

const tags: string = "  react\t typescript   node \n vite ";
console.log("spaces: ", tags.trim().split(/\s+/));

Output:

plain:   [ '', 'Denver', '', 'Boulder', '', 'Aurora', '' ]
with +:  [ '', 'Denver', 'Boulder', 'Aurora', '' ]
filter:  [ 'Denver', 'Boulder', 'Aurora' ]
spaces:  [ 'react', 'typescript', 'node', 'vite' ]
Command Prompt output of TypeScript split showing empty strings from repeated delimiters, the + quantifier, filter(Boolean) and splitting on whitespace
The + quantifier collapses repeats, but only filter(Boolean) drops the empty ends.

For any run of spaces, tabs and newlines, trim().split(/\s+/) is the reliable pattern. I go deeper on cleanup in removing empty strings from an array in TypeScript.

Split on multi-character separators with alternation

A character class only handles single characters. For longer separators, list the options with | inside a group. Word boundaries (\b) stop or matching inside “orange”:

const order: string = "bagels and coffee or tea, muffins -> receipt";

const items: string[] = order.split(/\s*(?:,|->|\band\b|\bor\b)\s*/);
console.log(items);

const log: string = "2026-10-19::Chicago::IL||$1,250.00";
console.log(log.split(/::|\|\|/));

Output:

[ 'bagels', 'coffee', 'tea', 'muffins', 'receipt' ]
[ '2026-10-19', 'Chicago', 'IL', '$1,250.00' ]

The (?:...) group is non-capturing on purpose; a plain (...) group would add the separators to the result, as you’ll see below. The same idea helps when you get the string after a character in TypeScript.

Escape special characters when you build the separator list

When the separators come from config or user input, you’ll build the regex with new RegExp(). Characters like ., | and + mean something in a regex, so escape them first, and sort longer separators first so :: wins over ::

function escapeRegExp(text: string): string {
  return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}

function splitOn(text: string, separators: readonly string[]): string[] {
  const sorted = [...separators].sort((a, b) => b.length - a.length);   // "::" before ":"
  const pattern = new RegExp(sorted.map(escapeRegExp).join("|"));
  return text.split(pattern);
}

const product: string = "SKU-100|Hat+Scarf::size:L";
const seps = ["|", "+", ":", "::"];

try {
  product.split(new RegExp(seps.join("|")));
} catch (err) {
  console.log("unescaped:", (err as Error).name, "-", (err as Error).message);
}
console.log("escaped:  ", splitOn(product, seps));

Output:

unescaped: SyntaxError - Invalid regular expression: /||+|:|::/: Nothing to repeat
escaped:   [ 'SKU-100', 'Hat', 'Scarf', 'size', 'L' ]

Unescaped, the + has nothing to repeat, so new RegExp() throws a SyntaxError; with other lists, . silently matches every character instead. If you need to swap characters instead of splitting, see replacing characters in a string using TypeScript.

Keep the separators in the result

Wrap the separator in a capturing group and split() places each matched separator between the pieces. A lookbehind keeps the delimiter attached to the piece before it instead:

const expr: string = "120+45-30*2";
console.log(expr.split(/([+\-*])/));        // capture group keeps the operators

const note: string = "Shipped to Miami. Paid $89.99! Arrives Friday?";
console.log(note.split(/(?<=[.!?])\s+/));    // lookbehind keeps the end mark

Output:

[
  '120', '+',
  '45',  '-',
  '30',  '*',
  '2'
]
[ 'Shipped to Miami.', 'Paid $89.99!', 'Arrives Friday?' ]
Command Prompt output of TypeScript split keeping math operators with a capture group and sentence punctuation with a lookbehind
The operators come back as their own elements; the sentence marks stay on their sentences.

That’s handy for tokenizing expressions or rebuilding the text with the join() method in TypeScript.

Can I pass an array of separators to split()?

No. TypeScript’s split() accepts a string, a RegExp or an object with a [Symbol.split] method, so an array won’t compile:

const csv: string = "Seattle,Portland;Boise";
const parts = csv.split([",", ";"]);

Output:

multi_array.ts(2,25): error TS2769: No overload matches this call.
  The last overload gave the following error.
    Argument of type 'string[]' is not assignable to parameter of type 'string | RegExp'.
Command Prompt showing the TypeScript compiler rejecting split called with an array of separators: No overload matches this call
Strict TypeScript catches the mistake before it runs.

Turn the array into a pattern with the splitOn() helper above instead.

Split by multiple delimiters without a regex

If you’d rather avoid regular expressions, normalize every separator to one with replaceAll() and split on that. The optional second argument caps how many pieces you keep:

const line: string = "Phoenix,Tucson;Mesa|Tempe";

const viaReplace = line.replaceAll(";", ",").replaceAll("|", ",").split(",");
console.log(viaReplace);

console.log("limit 2:", line.split(/[,;|]/, 2));

Output:

[ 'Phoenix', 'Tucson', 'Mesa', 'Tempe' ]
limit 2: [ 'Phoenix', 'Tucson' ]

Each new separator adds another call, so I use this for two or three fixed delimiters and switch to a regex beyond that.

Which split pattern fits your separators?

SeparatorsPatternExample
Single charactersCharacter class/[,;|]/
Separators with spaces around themClass plus \s*/\s*[,;]\s*/
Words or multi-character tokensAlternation/::|->|\band\b/
Any whitespace\s+ after trim()/\s+/
Keep the delimitersCapture group/([+\-*])/

Here are more string guides you might want next:

Frequently asked questions

How do I split a string by multiple characters in TypeScript?

Pass a character class to split(), for example text.split(/[,;|]/). Each listed character works as a separator.

How do I split on separators that are longer than one character?

Use alternation in a non-capturing group, such as text.split(/\s*(?:,|->|\band\b)\s*/).

How do I remove empty strings after splitting?

Add + to the pattern to merge repeated separators, then chain .filter(Boolean) to drop empty ends.

How do I split by multiple separators but keep them?

Put the separators in a capturing group, like /([+\-*])/. split() then includes each matche