Tuesday, February 2, 2010

Legal identifier matching regex pattern

The following will match any legal identifier — an unlimited-length sequence of Unicode letters and digits, beginning with a letter, the dollar sign "$", or the underscore character

/^[a-zA-Z\$_][a-zA-Z0-9]*/


The following is a example in Javascript

alert(/^[a-zA-Z\$_][a-zA-Z0-9]*/.test("testVar"));

3 comments:

Chintana Wilamuna said...

Small correction. This will not match any Unicode letters since you've specified a subset of ASCII :-)

chanaka said...

Thanks Chintana for the correction.

To simplify and correct the regex it can be written as

/^[\w\$][\w]*/

Since Firefox regex engine doesn't know Unicode \w is equivalent to [a-zA-Z0-9_]

bbeatifullife said...

I like your blog. Thank you. They are really great .
Some new style Puma Cat shoes is in fashion this year.
The Nike Air Shoes is a best Air Shoes .
Nike Air Rift is good and Cheap Nike Shoes.
If you are a fans of Puma basket,we would offer the good and Cheap Puma Shoes for you .the cheap ugg bootsis best christmas gift now.
The information age is really convenient .