Start a personal dev blog on your domain for free with Hashnode and grow your readership.
Get Started

Easy peasy split the string

The question is to split string in such a way that each element in the array has two characters and if the length of the string is odd then the missing character should get replaced by '_'.

Example:

input: "abc"

output: ["ab", "c_"]

input: "abcd"

output: ["ab", "cd"]

So the single line solution is:

const splitString2 = (str) => str.concat('_').match(/../g);

String.prototype.match() takes parameters as regular expressions object and returns an array whose value depends on presence and absence of g flag. The g flag returns all results matching the regex.

The dot(.) in regex represents any character except newline. Therefore, /../g represents two characters to match.

Happy coding!

Start a personal dev blog on your domain for free and grow your readership.

3.4K+ developers have started their personal blogs on Hashnode in the last one month.

Write in Markdown · Publish articles on custom domain · Gain readership on day zero · Automatic GitHub backup and more

No Comments Yet