JavaScript String Split: Syntax, Examples, and Tips

How-to basics · Updated November 22, 2026

In JavaScript, call string.split(separator) to divide a string into an array of substrings. The separator can be a literal string or a regular expression; for example, 'red,green,blue'.split(',') returns ['red', 'green', 'blue'].

What String Split Does

split() finds the separator in a string and returns the pieces around it as an array. It does not change the original string: JavaScript strings are immutable, so the method creates a result rather than editing its input. This is useful when text contains a consistent boundary, such as commas between simple values, spaces between words, or slashes in a date-like string.

For instance, splitting 'one/two/three' on '/' gives three strings. The separator itself is normally left out of the result. That makes split() a natural starting point for turning delimited text into values you can loop over, filter, or convert. The returned array can contain empty strings too, so inspect the input format rather than assuming every result is meaningful.

Syntax and Basic Examples

The method’s general form is text.split(separator, limit). The separator says where to divide the text, and the optional limit caps how many array elements are returned. Both arguments are optional, but omitting the separator has a specific result: the whole string is returned as a one-element array.

const phrase = 'JavaScript is useful';
const parts = phrase.split(' ');
// ['JavaScript', 'is', 'useful']

You can split on a multi-character separator as well. In 'name::role::status'.split('::'), the result is ['name', 'role', 'status']. Matching is literal when the separator is a string; punctuation such as a period has no special meaning in that case. If the separator does not occur, the result is generally an array containing the original string.

Calling split() with no argument, or with undefined, returns [text], not an array of characters. Passing '' is different: it splits a nonempty string between its UTF-16 code units. That distinction explains why a missing variable used as a separator can produce a surprising one-item array rather than character-by-character output.

Choosing a Separator

Use a string separator when the boundary is known exactly. Common examples include ',' for a simple comma-separated line, '/' for slash-delimited text, and '--' for a custom marker. A string separator is easy to read and appropriate when every boundary has the same spelling and no escaping rules complicate the data.

Use a regular expression when one boundary can take several forms. For example, 'a, b; c'.split(/[,;]? */) would be unnecessarily obscure; a clearer separator for commas or semicolons followed by optional spaces is /[,;]\s*/, producing ['a', 'b', 'c']. The regular expression lets the same call handle multiple delimiter characters and variable whitespace.

Remember that a regular expression’s syntax applies only when you pass a regular expression. text.split('.') treats the period as a literal period, while text.split(/./) uses the dot metacharacter, which matches characters. Use a regular-expression literal between slashes, or construct a RegExp deliberately when the pattern must be built dynamically.

Using the Limit Argument

The second argument limits the number of elements in the returned array; it does not mean “split at this many separators and then preserve the rest.” For example, 'a,b,c,d'.split(',', 2) returns ['a', 'b']. The later text is not included in another element. If you need the remainder intact, split first and combine the remaining array elements, or locate the boundary with methods such as indexOf() and slice().

The limit can be useful when only an initial portion matters, such as reading the first two fields of a simple record. It can also prevent creating an unnecessarily large result, but it is not a general input-size or security safeguard: the original string still exists, and the result may still need validation. A limit of zero returns an empty array. Values are converted to an unsigned 32-bit integer, so use a clear nonnegative integer rather than relying on unusual coercions.

Regular Expressions and Captured Delimiters

When a regular expression used as the separator contains capturing groups, the captured text is included in the returned array. For example, 'a,b;c'.split(/([,;])/) returns ['a', ',', 'b', ';', 'c']. This behavior can help when the delimiters themselves matter, but it can also add unexpected entries if a pattern contains parentheses intended only for grouping.

For grouping without capturing the matched delimiter, use a noncapturing group such as /(?:,|;)/. Regex lookarounds can also describe a boundary without consuming certain characters, though they can make patterns harder to maintain. Start with a simple string separator when it fits, and add regex features only when they make the intended rule clearer.

Regular-expression splitting has edge cases around matches at the start or end of a string and patterns that can match an empty string. If a result includes unexpected empty elements, inspect the pattern and test it with empty input, leading delimiters, and trailing delimiters. Do not assume that every regex separator behaves like removing one visible character.

Whitespace, Empty Values, and Cleanup

A common task is splitting words on whitespace. Using text.split(' ') only matches a single ordinary space. Multiple spaces create empty elements, and tabs or line breaks are not treated as that separator. A regular expression such as /\s+/ matches runs of whitespace, including common spaces, tabs, and line breaks.

const text = '  learn\tJavaScript  today  ';
const words = text.trim().split(/\s+/);
// ['learn', 'JavaScript', 'today']

trim() removes whitespace at the beginning and end before splitting, avoiding empty first or last elements in this example. There is one important exception: an empty or whitespace-only string becomes an empty string after trimming, and splitting that empty string on /\s+/ produces ['']. If the desired result for blank input is an empty array, check it explicitly or filter empty values:

const words = text.trim() ? text.trim().split(/\s+/) : [];

Filtering with .filter(Boolean) is another option when empty fields should always be discarded. However, empty fields can carry meaning. In 'a,,c'.split(','), the result is ['a', '', 'c'], which records that the middle field was blank. Removing it would shift the positions of later fields and could corrupt the interpretation.

Unicode and Character Splitting

Although split('') is often described as splitting into characters, JavaScript strings use UTF-16 code units. Some visible characters, including many emoji, use a pair of code units. Splitting an emoji with an empty-string separator can therefore break the pair and produce pieces that are not the intended visible character. A basic Unicode-aware alternative for iterating code points is [...text] or Array.from(text), but even these do not always keep a multi-code-point grapheme, such as a family emoji or a letter combined with a separate accent mark, together.

If an application needs user-perceived characters—for example, counting what a person sees as one symbol—use Intl.Segmenter with grapheme granularity in environments that support it. If the goal is simply to split at spaces or punctuation, keep using the relevant separator rather than splitting the entire string into characters. The right method depends on whether the task concerns code units, Unicode code points, or grapheme clusters.

Common Parsing Mistakes and Safer Approaches

split() divides text; it does not fully parse a data format. A basic comma split can fail on CSV fields that contain quoted commas, escaped quotation marks, or line breaks. For example, a record such as name,description may have a quoted description containing a comma, so splitting every comma changes the field structure. For real CSV files, use a CSV parser that follows the format’s quoting rules rather than trying to cover every case with a quick split.

Likewise, splitting a URL on '/' or a file path on a hard-coded separator can overlook encoding, query strings, platform differences, or special path rules. Use purpose-built APIs such as URL for URLs and the appropriate path utilities in the runtime for file paths. Treat split results as strings until you validate and convert them; for numeric fields, for example, parse the value and check that the conversion succeeded instead of assuming the text is a valid number.

  • Test empty input, repeated delimiters, and delimiters at the edges.
  • Check whether blank fields should be preserved or removed.
  • Validate the number and meaning of fields before using them.
  • Use a format-aware parser when the data has escaping or quoting rules.

Split Compared with Related Methods

split() is useful when you need all the pieces in an array. If you only need to find one boundary, indexOf() together with slice() may avoid creating an array of every field. If you want to replace separators rather than collect pieces, use replace() or replaceAll(). If the task is to check whether a separator exists, includes() may express that intent more directly.

For the reverse operation, array.join(separator) combines array elements into a string, placing the separator between them. Splitting and joining can be useful for simple transformations, but they are not automatically reversible: empty values, normalization, and quoting rules can affect what comes back. Choose the operation based on whether you need to search, transform, or represent structured data, not just because a string contains punctuation.

Frequently Asked Questions

Does JavaScript split change the original string?
No. Strings are immutable, and split() returns a new array. The original string remains unchanged unless your code assigns a different value to the variable.

How do I split a string into words?
For ordinary whitespace-separated words, use text.trim().split(/\s+/). If blank input should produce an empty array, check for an empty trimmed string before splitting.

How do I split on more than one delimiter?
Pass a regular expression, such as text.split(/[,;|]/) to split on commas, semicolons, or vertical bars. Escape characters that have special meaning in regular expressions when you intend them literally.

Why does split return empty strings?
Empty strings appear when delimiters are adjacent or at the beginning or end of the input. They can represent blank fields, so remove them only when the data rules say those fields should be ignored.

How do I split a string into characters without breaking emoji?
[...text] or Array.from(text) handles Unicode code points better than split(''), but neither always preserves complete user-perceived characters. Use Intl.Segmenter with grapheme granularity when that distinction matters and the target environment supports it.

Related

Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.

More in How-to basics

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.