Categories
JavaScript Answers

How to define a regular expression for not allowing spaces in the input field with JavaScript?

Spread the love

To define a regular expression for not allowing spaces in the input field with JavaScript, we use the \S pattern.

For instance, we write

const regex = /^\S*$/;

to define a regex that matches any non-space characters with the \S+ pattern.

And we use ^ to match the start of a string and $ the end of a string.

We use * to match repetition.

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 *