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.