I'm trying to create a regex that will match only when the string has anything but alphas, spaces, and hyphens. In other words, the string can only contain letters, spaces, and hyphens.
2 Answers
if(someString.match(/[a-z -]+/i){
// it's valid
}
3 Comments
brettkelly
"the string can only contain letters, spaces, and hyphens."
Eimantas
change regexp to this: /[^a-z \-]+/i
Ateş Göral
If you don't anchor your expression with ^ and $, this will simply match a substring. The rest of the string can still contain unwanted characters.