Categories
JavaScript Answers

How to split string with newline (‘\n’) in Node.js?

Spread the love

Sometimes, we want to split string with newline (‘\n’) in Node.js.

In this article, we’ll look at how to split string with newline (‘\n’) in Node.js.

How to split string with newline (‘\n’) in Node.js?

To split string with newline (‘\n’) in Node.js, we can use the string split method.

For instance, we write

const arr = "a\nb\r\nc".split(/\r?\n/);

to call "a\nb\r\nc".split with the regex /\r?\n/ to split the string by \r\n or \n into separate strings.

Conclusion

To split string with newline (‘\n’) in Node.js, we can use the string split method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *