Categories
JavaScript Answers

How to define regex for empty string or white space with JavaScript?

Spread the love

To define regex for empty string or white space with JavaScript, we can use the \s+ pattern.

For instance, we write

const regex = /^\s+$/;

to define a regex that matches a string with all spaces by using ^ to match the start of the string, \s+ to match spaces, and $ to match the end of the string.

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 *