Explain Codes LogoExplain Codes Logo

How can I split a string into segments of n characters?

javascript
regex-patterns
string-manipulation
javascript-features
Nikita BarsukovbyNikita Barsukov·Jan 5, 2025
TLDR

To split a string into n-sized parts, use:

const splitIntoSegments = (str, n) => str.match(new RegExp(`.{1,${n}}`, 'g')) || [];

To use this function:

const segments = splitIntoSegments("YourJavaScriptRocks", 4);

This function not only handles the usual string splitting, but also gracefully deals with special characters and empty strings.

Clear look at the magical RegEx

Getting into the groove with RegEx

The core engine for our tandem bike here is the regex pattern /.{1,n}/g. This snazzy pattern matches one to n characters in the string all the way to the end!

Embracing newlines and special characters

Changing gears to allow newlines and special characters? Replace the simple dot with [\s\S]:

const regexPattern = new RegExp(`[\\s\\S]{1,${n}}`, 'g'); const segments = herMajestyTheString.match(regexPattern) || [];

Villain null's got nothing!

Ever bumped into a null when your precious function found nothing to match? Fret not, || [] safeguards your functions, returning you an empty array instead.

Tailoring your suit (or in this case, your pattern!)

Potter around with the edge cases and meet odd lengths half-way. If the string length isn't playing ball with n, the last segment ensnares the remainder.

Non-regex approach: guys, it's not rocket science!

Good old for-loop: ever so reliable

Ring the bell and let's call the for-loop forward to help with substring extraction:

function splitStringNonRegex(str, n) { var result = []; // Looping over your string like an atypical JavaScript rockstar for (var i = 0; i < str.length; i += n) { result.push(str.substring(i, i + n)); } return result; // See result? It's the array. Not something to swear by, but hey, it works! }

The simplicity and clarity of this approach would make your coding grandma proud.

Think about this!

Arm wrestle with unicodes and emojis

JavaScript sings duets with UCS-2 encoding, meaning that special surrogate pairs may sneak past simple splitting. Keep libraries like lodash/chunk at the ready for all strings with complex characters such as emoji.

Regex: Ultron version

Lookbehind assertions, anyone? They stops breakages at specified points:

/(?<=^(?:.{n})+)(?!$)/

This pattern handles segmented data delicately but needs some JavaScript environments adjustments.

Going above and beyond

Wanna see your segments?

See what you’re working with:

const visualisedSegments = zeString.replace(new RegExp(`(.{${n}})`, 'g'), '$1-'); // Adding a dash - a line really - to your carpentry work.

Overlapping segments? No more!

Use non-capturing groups (?:...) to ensure discreet segmentation:

const pattern = new RegExp(`(?:.{${n}})(?!$)`, 'g'); // capturing n characters but not the string ending. Get it? Got it? Good!

Empty array magic

An empty array makes an amazing placeholder if you need your segments to perform tricks:

let segments = Array.from({ length: Math.ceil(str.length / n) }, () => ''); // Ready! Empty strings awaiting their debut.