Home >Web Front-end >JS Tutorial >How to Include a Hyphen in a Regex Character Class?

How to Include a Hyphen in a Regex Character Class?

Susan Sarandon
Susan SarandonOriginal
2024-11-29 17:06:10818browse

How to Include a Hyphen in a Regex Character Class?

Including a Hyphen in a Regex Character Bracket?

In a jQuery validation script, a custom validation method was introduced to allow only letters, numbers, and hyphens. However, upon testing, hyphens were being flagged as invalid.

To resolve this issue, character escaping with - is necessary to include the hyphen within the character class. Alternatively, placing the hyphen at the beginning or end of the character bracket can also alleviate the problem. The following regular expression will successfully include hyphens:

/^[a-zA-Z0-9._-]+$/

The above is the detailed content of How to Include a Hyphen in a Regex Character Class?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn