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!