Categories
JavaScript Answers

How to use JavaScript regex to split by line?

Spread the love

Sometimes, we want to use JavaScript regex to split by line.

In this article, we’ll look at how to use JavaScript regex to split by line.

How to use JavaScript regex to split by line?

To use JavaScript regex to split by line, we can call the string split method with a regex.

For instance, we write

const result = subject.split(/\r?\n/);

to call subject.split with a regex that matches \r\n or \n and return an array with the substrings between the new line characters.

Conclusion

To use JavaScript regex to split by line, we can call the string split method with a regex.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.