Categories
JavaScript Answers

How to split string with white space with JavaScript?

Spread the love

To split string with white space with JavaScript, we use the split method.

For instance, we write

const str = "my car is green";
const stringArray = str.split(/(\s+)/);

to call str.split with a regex that matches whitespaces to split the str string by the whitespaces and return an array with the split substrings.

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 *