Categories
JavaScript Answers

How to split a string into segments of n characters with JavaScript?

Spread the love

To split a string into segments of n characters with JavaScript, we call the match method.

For instance, we write

const str = "abcdefghijkl";
console.log(str.match(/.{1,3}/g));

to call str.match with a regex the matches groups of 1 to 3 characters in the str array.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *