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.
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.
I tried using following regex:
/^\w+([\s-_]\w+)*$/
This allows alphanumeric, underscore, space and dash. More details
Help me with my regex that only accepts alpha characters, hyphens, and spaces with a minimum of seven characters please
regex - Regular Expression for AlphaNumeric, Hyphen and Underscore without any space - Stack Overflow
[SOLVED] Alphanumeric plus hypens, periods, and underscores.
Regular expression to match alphanumeric, hyphen, underscore and space string
However, the code below allows spaces.
No, it doesn't. However, it will only match on input with a length of 1. For inputs with a length greater than or equal to 1, you need a + following the character class:
var regexp = /^[a-zA-Z0-9-_]+$/;
var check = "checkme";
if (check.search(regexp) === -1)
{ alert('invalid'); }
else
{ alert('valid'); }
Note that neither the - (in this instance) nor the _ need escaping.
This is the most concise syntax I could find for a regex expression to be used for this check:
const regex = /^[\w-]+$/;
Hi
I am working with a regex that does what the title says, but it doesn't work if there is a space in the middle of the word
My regex:
^([A-Za-z0-9-]){1,7}$
Word example, delete the double quotes:
-
"-as5- asd"
-
"-50 as 8963."
Try the regex here
https://regexr.com/
Working
Here is the quick solution.
String regex = "^[a-zA-Z0-9_-]*$";
sampleString.matches(regex);
(^) anchor means start of the string
($) anchor means end of the string
A-Z , a-z represent range of characters
0-9 represent range of digit
( _ ) represent underscore
( - ) represent hyphen
You can use ^[\w-]*$ where \w means:
- any letter in the range
a-z,A-Z, - any digit from
0 to 9, underscore
The error you made was to encapsulate you correct regex ([a-zA-Z0-9_-]* == [\w-]*) within a character class, loosing the quantifier (*) meaning
Character sets will help out a ton here. You want to create a matching set for the characters that you want to validate:
- You can match alphanumeric with a
\w, which is the same as[A-Za-z0-9_]in JavaScript (other languages can differ). - That leaves
-and spaces, which can be combined into a matching set such as[\w\- ]. However, you may want to consider using\sinstead of just the space character (\salso matches tabs, and other forms of whitespace)- Note that I'm escaping
-as\-so that the regex engine doesn't confuse it with a character range likeA-Z
- Note that I'm escaping
- Last up, you probably want to ensure that the entire string matches by anchoring the start and end via
^and$
The full regex you're probably looking for is:
/^[\w\-\s]+$/
(Note that the + indicates that there must be at least one character for it to match; use a * instead, if a zero-length string is also ok)
Finally, http://www.regular-expressions.info/ is an awesome reference
Bonus Points: This regex does not match non-ASCII alphas. Unfortunately, the regex engine in most browsers does not support named character sets, but there are some libraries to help with that.
For languages/platforms that do support named character sets, you can use /^[\p{Letter}\d\_\-\s]+$/
Try this regex:
/^[a-z\d\-_\s]+$/i
To match a string that contains only those characters (or an empty string), try
"^[a-zA-Z0-9_]*$"
This works for .NET regular expressions, and probably a lot of other languages as well.
Breaking it down:
^ : start of string
[ : beginning of character group
a-z : any lowercase letter
A-Z : any uppercase letter
0-9 : any digit
_ : underscore
] : end of character group
* : zero or more of the given characters
$ : end of string
If you don't want to allow empty strings, use + instead of *.
As others have pointed out, some regex languages have a shorthand form for [a-zA-Z0-9_]. In the .NET regex language, you can turn on ECMAScript behavior and use \w as a shorthand (yielding ^\w*$ or ^\w+$). Note that in other languages, and by default in .NET, \w is somewhat broader, and will match other sorts of Unicode characters as well (thanks to Jan for pointing this out). So if you're really intending to match only those characters, using the explicit (longer) form is probably best.
There's a lot of verbosity in here, and I'm deeply against it, so, my conclusive answer would be:
/^\w+$/
\w is equivalent to [A-Za-z0-9_], which is pretty much what you want (unless we introduce Unicode to the mix).
Using the + quantifier you'll match one or more characters. If you want to accept an empty string too, use * instead.
Character sets will help out a ton here. You want to create a matching set for the characters that you want to validate:
- You can match alphanumeric with a
\w, which is the same as[A-Za-z0-9_]in JavaScript (other languages can differ). - That leaves
-and spaces, which can be combined into a matching set such as[\w\- ]. However, you may want to consider using\sinstead of just the space character (\salso matches tabs, and other forms of whitespace)- Note that I'm escaping
-as\-so that the regex engine doesn't confuse it with a character range likeA-Z
- Note that I'm escaping
- Last up, you probably want to ensure that the entire string matches by anchoring the start and end via
^and$
The full regex you're probably looking for is:
/^[\w\-\s]+$/
(Note that the + indicates that there must be at least one character for it to match; use a * instead, if a zero-length string is also ok)
Finally, http://www.regular-expressions.info/ is an awesome reference
Bonus Points: This regex does not match non-ASCII alphas. Unfortunately, the regex engine in most browsers does not support named character sets, but there are some libraries to help with that.
For languages/platforms that do support named character sets, you can use /^[\p{Letter}\d\_\-\s]+$/
Try this regex:
/^[a-z\d\-_\s]+$/i
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.
Check this working in fiddle http://refiddle.com/refiddles/56a07cec75622d3ff7c10000
This will fix the issue
^[a-zA-Z]+[a-zA-Z0-9-_ ]*[a-zA-Z0-9]$