How to call split with regex in JavaScript?

Sometimes, we want to call split with regex in JavaScript.

In this article, we’ll look at how to call split with regex in JavaScript.

How to call split with regex in JavaScript?

To call split with regex in JavaScript, we call split with a regex to split by any substring that matches the pattern.

For instance, we write

const rawElements = str.split(new RegExp("[,;n]", "g"));

to call str.split with new RegExp("[,;n]", "g") to split the str string by commas, semicolons, and newlines.

We use the 'g' flag to match all instances.

The split strings will be returned in an array.

Conclusion

To call split with regex in JavaScript, we call split with a regex to split by any substring that matches the pattern.