JavaScript

JavaScript: Split string and keep the separators

Advertisements

String.prototype.split() is a valuable method to split strings based on a delimiter. There often comes a scenario when we want to split a string and keep the separators in the result. The same JavaScript method provides a way to do so.

Before we get into that, for people who are unfamiliar with split(), here’s a quick refresher.

Split()

The function can be called on a string with two parameters. The first is the separator on which we want to split the input string. And the second is the limit which is an optional parameter and specifies the number of times that the separator should be matched.

The separator can be a string or a regex.

const inputString = 'The quick brown fox jumps over the lazy dog.';
console.log(inputString.split(" "));
//  ['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog.']
JavaScript

Using regex for the separator:

console.log(inputString.split(/ /));
// ['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog.']
JavaScript

Splitting and keeping the separators

As the MDN docs for Split() state:

When found, separator is removed from the string, and the substrings are returned in an array.

But there is a workaround for regular expressions. Using positive lookaheads, we can assert that the regular expression exists, but not actually match it. In simpler words, if parenthesis, that is ( and ), are used in the separator, matched results are included in the array.

const inputString = 'Hello 1 word. Sentence number 2.'
const splits = inputString.split(/(\d)/)

console.log(splits)
// [ "Hello ", "1", " word. Sentence number ", "2", "." ]
JavaScript

Note: \d matches the character class for digits between 0 and 9.

Thus we can use lookarounds to separate strings and keep the separators too! This opens up easier ways to solve some string-parsing problems.

Saransh Kataria

Born in Delhi, India, Saransh Kataria is the brain behind Wisdom Geek. Currently, Saransh is a software developer at a reputed firm in Austin, and he likes playing with new technologies to explore different possibilities. He holds an engineering degree in Computer Science. He also shares his passion for sharing knowledge as the community lead at Facebook Developer Circle Delhi, NCR which is a developer community in Delhi, India.

Share
Published by
Saransh Kataria
Tags: javascript

Recent Posts

Remapping keyboard keys to avoid Carpal Tunnel

I am terrible at optimizing my keyboard layout for anything. But off lately, my little…

1 month ago

Fixing cookies are blocked for a website with shields down on Brave

I recently switched completely to the Brave browser and have set ad blocking to aggressive…

5 months ago

Generating a QR code using Node.js

I was preparing a slide deck for a hackathon and decided to put in a…

6 months ago

How to clear the global npx cache

I have been using npx a lot lately, especially whenever I want to use a…

6 months ago

Copy/Pasting output from the terminal

Manually copy-pasting the output of a terminal command with a mouse/trackpad feels tedious. It is…

7 months ago

How To Get The Hash of A File In Node.js

While working on a project, I wanted to do an integrity check of a file…

8 months ago
Advertisements