[Project_owners] Re: Validating javascript identifiers

Eric T. cougio at gmail.com
Thu Sep 8 20:15:40 EDT 2005


> are you trying to validate/verify  javascript?

No not javascript code itself (something I would like to do eventually
but right now I try {} it and if it throws up well that's just too
bad...).

What I'm trying to do right now is just validate that something is a
"word" (in any language), as defined by whatever version of the
unicode standard is used by mozilla, and for convenience (being able
to simply create an object with that word as identifier instead of
random crap I then have to map), verifying that it only contains
letters valid in a javascript identifier as defined by whatever weird
mix of js 1.5, 2.0 and various ecma script versions the current
SpiderMonkey implements would be even better...

I need this for several parts of my code and sometimes restricting to
characters that are valid in an arbitrary charset, so the more generic
solution is better... part is parsing user input strings looking for
user defined "variables" (while not a js identifier I want it to have
pretty much the same rules and tie it to a function, so using that
"word" as function identifier would keep things simple) surrounded by
a non word prefix and a \b or suffix.

For testing I'm using "[a-zA-Z]\\w*", but that's total crap... The
simplest solution I found is "(?:[A-Za-z]|[^\\x00-\\x7F])\\w*", which
matches any ascii letter and any non ascii unicode character...
*might* be good enough for string "variables", but not identifiers...

My ideal solution is doing it with a regexp since that's what I use to
look for the var in the first place, but if there's an xpcom component
that does this, doing some more validation manually on a match would
work too, maybe charset detection stuff can help?


More information about the Project_owners mailing list