How to Split a String in Node.js?

In this tutorial, I am going to walk you through how you can Split a String in Node.js, or JavaScript.

It might seem like a useless feature to you when you’re just starting off with JavaScript. However, as you advance with your knowledge in this language, and start working on authentication (and many other things), you will realize how much importance it holds.

We are going to discuss the split() method in Node.js today.

What is the split() Method?

The split() method in Node.js or JavaScript, divides a string value (plain text value) into substrings, stores them in an array, and returns that array.

The string is divided based on a pattern which is passed in the first argument when calling the method.

The syntax looks like this:

split(separator, [limit])

Arguments/Parameters accepted

separator – accepts a character or a regular expression. If nothing passed, it will return the entire string inside an array.

[limit] – a limit that you may specify as an integer. It is optional.

The split() method will not change the original string in any way. Which means you can access it over and over again.

If we pass a blank separator to this method, it will perform a character split. Meaning, every character inside the string is separated.

When we pass a space character to the separator, it will perform a words split.

Getting Started Splitting a String

This method is only available on strings. A string may be situated anywhere. For example, simply in a variable, or in an array, or stored in an object.

Split a string into variables

Let us split every character in a string that is stored in a studentName variable, using the split() method.

const studentName = 'Sarah Lee';
studentName.split('');
// ["S", "a", "r", "a", "h", " ", "L", "e", "e"]

Next, let’s pass a string with a space character:

studentName.split(' ');
// ["Sarah", "Lee"]

Now, let us pass a limit to split our characters.

//Limit for character split
studentName.split('', 4);
// ["S", "a", "r", "a"]
// Let’s add more words and test out words split
const sarahZodiac = 'Sarah is a Gemini'
sarahZodiac.split(' ', 2)
// ["Sarah", "is"]

Split a String from an Array

Let’s say we have an array of names with us, saved like this:

const nameList = ['Tarry Wood', 'Aneesha Shaikh', 'Bamidele Onibalusi'];

To select a name, we will first select the array and then one of the strings using the index.

nameList[2].split('')
// ["B", "a", "m", "i", "d", "e", "l", "e", " ", "O", "n", "i", "b", "a", "l", "u", "s", "i"]

Click here to read How to Remove a Particular Element from an Array in JavaScript.

Split a String from an Object

Let us create a new productList object, for this example:

const productList = { name: 'Nike Air Force 1', price: '$109.5', category: 'Women'}
productList.name.split(' ', 3)
// ["Nike", "Air", "Force"]

Passing Characters to the Separator Parameter

So, we haven’t yet tried passing any characters to the separator parameter. Let us take a look at that.

const nikeShoes = 'Nike Air Force 1'
nikeShoes.split('Nik')
// ["", "e Air Force 1"]

The output we have here indicates the characters we specified in the separator argument, have been chopped off of the string and split in half.

So, these are all the ways you can split a string.

Conclusion

The Split method in JavaScript is useful for developers to split a string, be it stored in a variable, in an array, or an object. This method has prominent use cases like in authentication and more.

Noteworthy References

Split String – MDN Official Docs

Split Method in JavaScript – W3 Schools Official Docs

Aneesha S
Aneesha S
Articles: 173