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.
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.']
JavaScriptUsing regex for the separator:
console.log(inputString.split(/ /));
// ['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog.']
JavaScriptAs the MDN docs for Split() state:
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", "." ]
JavaScriptNote: \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.
I am terrible at optimizing my keyboard layout for anything. But off lately, my little…
I recently switched completely to the Brave browser and have set ad blocking to aggressive…
I was preparing a slide deck for a hackathon and decided to put in a…
I have been using npx a lot lately, especially whenever I want to use a…
Manually copy-pasting the output of a terminal command with a mouse/trackpad feels tedious. It is…
While working on a project, I wanted to do an integrity check of a file…