JavaScript String.Split() Example with RegEx
Last Updated :
07 Dec, 2024
Improve
The String.split() method in JavaScript is used to divide a string into an array of substrings. While it is often used with simple delimiters like spaces or commas, you can use Regular Expressions (RegEx) for more advanced and flexible string splitting.
Syntax
string.split(separator, limit)
- separator: This can be a string or a regular expression that defines where to split the string.
- limit: (Optional) Specifies the maximum number of substrings to include in the array.
1. Split on a Single Character
let s = "apple-banana-cherry";
let res = s.split(/-/);
console.log(res);
Output
[ 'apple', 'banana', 'cherry' ]
- The regular expression /-/ matches the hyphen (-) in the string.
- The string is split at each occurrence of the hyphen.
2. Split on Multiple Delimiters
let s = "apple, banana; cherry|grape";
let res = s.split(/[,;|]/);
console.log(res);
Output
[ 'apple', ' banana', ' cherry', 'grape' ]
- The regular expression /[,;|]/ matches commas, semicolons, or vertical bars.
- The string is split at each occurrence of any of these characters.
3. Split and Remove Extra Whitespace
let s = " apple banana cherry ";
let res = s.split(/\s+/);
console.log(res);
Output
[ '', 'apple', 'banana', 'cherry', '' ]
- \s+: Matches one or more whitespace characters.
- The string is split at spaces, tabs, or newlines.
4. Split by Word Boundaries
let s = "HelloWorld";
let res = s.split(/(?=[A-Z])/);
console.log(res);
Output
[ 'Hello', 'World' ]
- (?=[A-Z]): Matches a position before any uppercase letter.
- The string is split into "Hello" and "World" based on word boundaries.
5. Split and Keep the Delimiters
let s = "one, two; three";
let res = s.split(/([,;])/);
console.log(res);
Output
[ 'one', ',', ' two', ';', ' three' ]
- ([,;]): Captures the delimiters (commas and semicolons) using parentheses.
- The captured delimiters are included in the resulting array.
6. Limit the Number of Splits
let s = "apple-banana-cherry-grape";
let res = s.split(/-/, 2);
console.log(res);
Output
[ 'apple', 'banana' ]
- The limit argument restricts the output to 2 substrings.
- The string is split at the first two occurrences of the hyphen.
7. Split on Digits
let s = "apple1banana2cherry3grape";
let res = s.split(/\d/);
console.log(res);
Output
[ 'apple', 'banana', 'cherry', 'grape' ]
- \d: Matches any digit (0–9).
- The string is split at every digit.
8. Split on a Custom Pattern (Email Example)
let s = "username@gmail.com";
let res = s.split(/[@.]/);
console.log(res);
Output
[ 'username', 'gmail', 'com' ]
- [@.]: Matches the "@" symbol or a period.
- The email is split into its username, domain, and extension.
9. Remove Empty Strings in the Result
If splitting leaves empty strings, you can filter them out.
let s = "apple,,,banana,,cherry";
let res = s.split(/,+/).filter(Boolean);
console.log(res);
Output
[ 'apple', 'banana', 'cherry' ]
- /,+/: Matches one or more commas.
- .filter(Boolean): Removes empty strings from the resulting array.
10. Split Multiline String
let s = `apple
banana
cherry`;
let res = s.split(/\n/);
console.log(res);
Output
[ 'apple', 'banana', 'cherry' ]
- \n: Matches a newline character.
- The string is split into separate lines.
When to Use String.split() with RegEx
- When splitting requires matching complex patterns (e.g., multiple delimiters, whitespace, word boundaries).
- When you need flexibility, such as capturing or excluding delimiters.
- For advanced text processing tasks like parsing structured data.
Performance Considerations
- Regular expressions in split() are powerful but can be slower for large strings or complex patterns.
- If performance is critical, test different approaches to find the most efficient solution.