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.

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 *