Categories
JavaScript Answers

How to include a hyphen in a regex character bracket with JavaScript?

Spread the love

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 \-.

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 *