Categories
JavaScript Answers

How to write regex for allowing alphanumeric, – , _ and space with JavaScript?

Spread the love

Sometimes, we want to write regex for allowing alphanumeric, – , _ and space with JavaScript.

In this article, we’ll look at how to write regex for allowing alphanumeric,- , _ and space with JavaScript.

How to write regex for allowing alphanumeric, – , _ and space with JavaScript?

To write regex for allowing alphanumeric, – , _ and space with JavaScript, we use various patterns.

For instance, we write

const re = /^[\w\-\s]+$/;

to define the regex to match alphanumeric, – , _ and space.

\w matches alphanumeric characters and _.

\- matches -,

\s matches spaces.

^ indicates the start of a word.

$ indicates the end of a word.

Conclusion

To write regex for allowing alphanumeric, – , _ and space with JavaScript, we use various patterns.

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 *