How to Split a String by a Regex in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 4, 2024
3 min

banner

# Split a String by a Regex in JavaScript

Pass a regular expression as a parameter to the String.split() method to split a string by a regex.

The split method takes a string or regular expression and splits the string based on the provided separator, into an array of substrings.

index.js
const str = 'one,two.three four'; const result = str.split(/[,.\s]/); // ๐Ÿ‘‡๏ธ ['one', 'two', 'three', 'four'] console.log(result);

split string by regex

The code for this article is available on GitHub

The String.split() method takes a separator and splits the string into an array on each occurrence of the provided delimiter.

The String.split() method takes the following 2 parameters:

NameDescription
separatorThe pattern describing where each split should occur.
limitAn integer used to specify a limit on the number of substrings to be included in the array.

The only argument we passed to the split() method is a regular expression.

The method splits the string into an array of substrings on each match of the regular expression.

index.js
const str = 'one,two.three four'; const result = str.split(/[,.\s]/); // ๐Ÿ‘‡๏ธ ['one', 'two', 'three', 'four'] console.log(result);

The forward slashes / / mark the beginning and end of the regular expression.

The regular expression in the example uses a character class [] to match any of the characters between the square brackets - a comma, a dot, and a whitespace.

Character classes are most commonly used when you need to split the string using multiple separators.

# Examples of splitting strings by a regex

Here's another example that splits a string on each digit.

index.js
const str = 'bobby1hadz2com'; const result = str.split(/\d/); console.log(result); // ๐Ÿ‘‰๏ธ [ 'bobby', 'hadz', 'com' ]

examples of splitting strings by regex

The code for this article is available on GitHub

The \d special character matches any digit 0-9. It's the same as using a range [0-9].

You don't have to pass the g (global) flag when using the split method with a regular expression. This is because the method doesn't stop on the first occurrence.

# Performing a case-insensitive match and split

However, you could use other flags. For example, you can use the i flag for a case-insensitive match.

index.js
const str = 'oneAtwoBthree'; const result = str.split(/[ab]/i); console.log(result); // ๐Ÿ‘‰๏ธ ['one', 'two', 'three']

performing case insensitive match and split

The code for this article is available on GitHub

The character class [] matches either a or b.

We set the i flag, so we match uppercase and lowercase occurrences of the a and b characters in the string.

If you ever need help reading a regular expression, check out this regular expression cheat sheet by MDN.

It contains a table with the name and the meaning of each special character with examples.

# Removing empty strings from the result

If you have leading or trailing matches or multiple matches next to one another, you might get empty string elements in the result.

index.js
const str = 'one,,two..three four.'; const result = str.split(/[,.\s]/); // ๐Ÿ‘‡๏ธ [ 'one', '', 'two', '', 'three', 'four', '' ] console.log(result);

removing empty strings from the result

The code for this article is available on GitHub

You can use the Array.filter() method to remove the empty strings from the array.

index.js
const str = 'one,,two..three four.'; const result = str.split(/[,.\s]/).filter(element => element); // ๐Ÿ‘‡๏ธ [ 'one', 'two', 'three', 'four' ] console.log(result);

The function we passed to the Array.filter() method gets called with each element in the array.

The filter() method returns a new array that only contains the elements that meet the condition.

On each iteration, we return the string as is to filter out all falsy values (empty strings) from the array.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev