Use a simple character class wrapped with letter chars:

^a-zA-Z?$

This matches input that starts and ends with a letter, including just a single letter.

There is a bug in your regex: You have the hyphen in the middle of your characters, which makes it a character range. ie [9-_] means "every char between 9 and _ inclusive.

If you want a literal dash in a character class, put it first or last or escape it.

Also, prefer the use of \w "word character", which is all letters and numbers and the underscore in preference to [a-zA-Z0-9_] - it's easier to type and read.

Answer from Bohemian on Stack Overflow
๐ŸŒ
Microsoft
social.msdn.microsoft.com โ€บ Forums โ€บ en-US โ€บ 41ea463f-e092-49d6-9a51-80d893e316f5
Regex check for alphanumeric, hyphen, underscore, space | Microsoft Learn
November 15, 2021 - I'd like to expand the Regex below to allow not only alphanumeric characters, but also hyphen, underscore, space. ... Ideally, I would like to allow only a single hyphen, underscore, or space without another hyphen, underscore, or space following, e.g., "my- name", "my--name", and "my-_name" would all return false. Any help from a regular expression ...
Discussions

Help me with my regex that only accepts alpha characters, hyphens, and spaces with a minimum of seven characters please
Add a space into the [A-Za-z0-9-] block, like [A-Za-z0-9- ] More on reddit.com
๐ŸŒ r/learnprogramming
7
6
November 4, 2022
regex - Regular Expression for AlphaNumeric, Hyphen and Underscore without any space - Stack Overflow
I would like to have a regular expression that checks if the string contains Alphanumerics, Hyphen and Underscore. There should not be any spaces or other special characters other these three. My s... More on stackoverflow.com
๐ŸŒ stackoverflow.com
[SOLVED] Alphanumeric plus hypens, periods, and underscores.
I'm working on a CMS that requires to download MySpace profile pages. The URL format is standard, except for the username portion. The username can contain letters, numbers, hyphens, and periods. Valid _n.E.E.d.l.e_ _____NEEDLE-123 _-5678...needle-_ Invalid n e e d l e !@#$%^&* needle'd! I wa... More on forums.phpfreaks.com
๐ŸŒ forums.phpfreaks.com
12
August 10, 2009
Regular expression to match alphanumeric, hyphen, underscore and space string
Regular expression to match alphanumeric, hyphen, underscore and space stringI'm trying to match a string that contains alphanumeric, hyphen, underscore More on exchangetuts.com
๐ŸŒ exchangetuts.com
2
446
January 21, 2016
๐ŸŒ
Devgex
devgex.com โ€บ en โ€บ article โ€บ 00026020
JavaScript Regular Expressions: Complete Guide to Validating Alphanumeric, Hyphen, Underscore, and Space Characters - DevGex
November 25, 2025 - Character sets form the core component ... hyphens, underscores, and spaces, precise matching can be achieved by combining different character classes....
๐ŸŒ
xjavascript
xjavascript.com โ€บ blog โ€บ regex-for-allowing-alphanumeric-and-space
JavaScript/jQuery Regex: How to Allow Alphanumeric, Hyphens, Underscores, and Spaces โ€” xjavascript.com
Alphanumerics: A-Z, a-z, 0-9 (covered by [A-Za-z0-9]). Underscores: _ (added directly to the character class). Hyphens: - (escaped as \- to avoid unintended ranges). Spaces: (added directly).
Find elsewhere
๐ŸŒ
Regex Tester
regextester.com โ€บ 110055
Regex allow only alphanumeric, white spaces and hyphen - Regex Tester/Debugger
Regular Expression to - Numeric: [0-9], - White spaces: [( )] - Hyphen: [-] ^[....]$: only get rows truth. + : check each rows
๐ŸŒ
PHP Freaks
forums.phpfreaks.com โ€บ php coding โ€บ regex help
[SOLVED] Alphanumeric plus hypens, periods, and underscores. - Regex Help - PHP Freaks
August 10, 2009 - I'm working on a CMS that requires to download MySpace profile pages. The URL format is standard, except for the username portion. The username can contain letters, numbers, hyphens, and periods. Valid _n.E.E.d.l.e_ _____NEEDLE-123 _-5678...needle-_ Invalid n e e d l e !@#$%^&* needle'd! I wa...
๐ŸŒ
The Full-Stack Blog
coding-boot-camp.github.io โ€บ full-stack โ€บ computer-science โ€บ regex-tutorial
Regular Expression Tutorial | The Full-Stack Blog
In bracket expressions, special characters that we want to include follow alphanumeric character ranges within the brackets. If we put all of these expressions together so that our pattern is [a-z0-9_-], this will match any string that includes any combination of lowercase letters between a ...
๐ŸŒ
RegExr
regexr.com โ€บ 3a8p3
RegExr: alphanumeric, underscore and .
RegExr is an online tool to learn, build, & test Regular Expressions (RegEx / RegExp).
๐ŸŒ
PHP Freaks
forums.phpfreaks.com โ€บ php coding โ€บ regex help
Allow only letters, numbers, dash, underscore, or space - Regex Help - PHP Freaks
June 11, 2014 - Regex always kills me. I want a string to be only letters, numbers, dash, underscore, or space. if ( !preg_match( $pattern, $str ) ){ $err = 'Only letters, numbers, space, underscore and dash are allowed.'; return $err } In this example, what is $pattern ? My best guess was... $pattern = '/[^a-zA...
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 608867 โ€บ java โ€บ Regex-alpha-numeric-apostrophes-hyphens
Regex for alpha numeric ,apostrophes and hyphens with certian conditions (Java in General forum at Coderanch)
April 5, 2013 - Henry Wong wrote: Perhaps the Oracle ... 'space' to the allowable set of characters then to use a single regex will likely require the use of 'zero-width negative lookahead' which is normally considered an advanced regex topic....
๐ŸŒ
ServiceNow Community
servicenow.com โ€บ community โ€บ developer-forum โ€บ need-regex-for-following-condition โ€บ m-p โ€บ 1373658
Solved: Need regex for following condition - ServiceNow Community
February 24, 2022 - Solved: Hi All, Please help me to create a regex for the below condition. The field only allows alphanumeric characters, periods, underscores,