简体   繁体   中英

Javascript regular expressions match alphabetical or -

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM