ismail ghedamsi
ismail ghedamsi

Reputation: 35

Javascript regular expressions match alphabetical or -

I wanna catch words that begin with an uppercase followed with either [a-b] or - but it don't seem to work. Here my regular expression I tried ^[A-Z][-[a-z]] How can I fix that?

Upvotes: 0

Views: 42

Answers (2)

Chris Rouffer
Chris Rouffer

Reputation: 763

You have an extra set of brackets in there:

var str = "A-abcd";
var str2 = "AA-abcd";

var patt = new RegExp("^[A-Z][-a-z]");
var res = patt.test(str);
console.log(res);  // True

var res = patt.test(str2);
console.log(res);  // False

Upvotes: 0

fubar
fubar

Reputation: 17378

You're close. You just have one too many sets of square brackets.

/^[A-Z][a-z\-]/

Upvotes: 1

Related Questions