I am currently available for freelance/contract work. Book a meeting so we can talk about your project.

Splitting strings by lengths

Posted on

JavaScript can split strings at given characters. By matching against a regular expression, we can split strings into substrings of a given length instead.

// split at every `O`; all `O`s are gone in the result
"BRBIDKLOLOMGOK".split("O")  // ⇒ ["BRBIDKL", "L", "MG", "K"]
 
// split into groups of up to 3 characters (the last group can be shorter)
"BRBIDKLOLOMGOK".match(/.{1,3}/g)  // ⇒ ["BRB", "IDK", "LOL", "OMG", "OK"]
// split at every `O`; all `O`s are gone in the result
"BRBIDKLOLOMGOK".split("O")  // ⇒ ["BRBIDKL", "L", "MG", "K"]
 
// split into groups of up to 3 characters (the last group can be shorter)
"BRBIDKLOLOMGOK".match(/.{1,3}/g)  // ⇒ ["BRB", "IDK", "LOL", "OMG", "OK"]
Debug
none
Grid overlay