Splitting in JavaScript

From the JavaScript Regular Expressions cheat sheet · String Methods · verified Jul 2026

Splitting

Using regex to split strings

javascript
// Basic split with regex
"a,b;c:d".split(/[,;:]/)          // ['a','b','c','d']

// Split with capture groups (includes separators)
"a1b2c3".split(/(\d)/)
// ['a','1','b','2','c','3','']

// Split with limit
"a-b-c-d".split(/-/, 2)           // ['a','b']

// Split on word boundaries
"HelloWorldTest".split(/(?=[A-Z])/)
// ['Hello','World','Test']

// Split but keep delimiters
"1+2-3*4".split(/(?=[+\-*])|(?<=[+\-*])/)
// ['1','+','2','-','3','*','4']

// Remove empty strings
",,a,,b,c,,".split(/,/).filter(Boolean)
// ['a','b','c']
💡 Capture groups in split() include separators in result
⚡ Use lookahead/behind to split without losing delimiters
📌 filter(Boolean) removes empty strings from result
🟢 Useful for parsing CSV, commands, or structured text
splitparsing

More JavaScript tasks

Back to the full JavaScript Regular Expressions cheat sheet