JavaScript: Split a String Using Regular Expressions

In this tutorial, we will introduce you how to split a string using regular expressions in javascript. We will use split() method to implement it.

JavaScript - Split a string using regular expressions

1.Create a string

const paragraph = 'The Answer to the Ultimate Question of Life, the Universe, and Everything is 42. Forty two. That's all there is.';

2.Split string by sentences

// Split by sentences
const sentences = paragraph.split(/[!?.]/);
console.log(sentences[1]);

Here /[!?.]/ is the regular expression.