Sometimes, we want to include a hyphen in a regex character bracket with JavaScript.
In this article, we’ll look at how to include a hyphen in a regex character bracket with JavaScript.
How to include a hyphen in a regex character bracket with JavaScript?
To include a hyphen in a regex character bracket with JavaScript, we can use \-
.
For instance, we write
/^[a-zA-Z0-9.\-_]+$/
to add hyphen into the list of characters to match in the square brackets.
Conclusion
To include a hyphen in a regex character bracket with JavaScript, we can use \-
.