Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Regex (string should include only alpha, space, hyphen)

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.

like image 928
Kyle Hayes Avatar asked Sep 11 '09 20:09

Kyle Hayes


1 Answers

If you are looking for a test for validity:

// from string start to end, only contains '-' "whitespace" or 'a'-'z' 
someString.match(/^[-\sa-zA-Z]+$/) 

Or the negation:

// has some invalid character not '-' "whitespace" or 'a'-'z'
someString.match(/[^-\sa-zA-Z]/) 
like image 105
gnarf Avatar answered Sep 28 '22 10:09

gnarf