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
Release: 2024-11-29 17:06:10
Original
812 people have browsed it

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._-]+$/
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template