Validate a JavaScript function name

Viewed 30611

What would be a regular expression which I can use to match a valid JavaScript function name...

E.g. myfunction would be valid but my<\fun\>ction would be invalid.

[a-zA-Z0-9_])?
6 Answers

This is more complicated than you might think. According to the ECMAScript standard, an identifier is:

an IdentifierName that is not a ReservedWord

so first you would have to check that the identifier is not one of:

instanceof typeof break do new var case else return void catch finally
continue for switch while this with debugger function throw default if
try delete in

and potentially some others in the future.

An IdentifierName starts with:

a letter
the $ sign
the _ underscore

and can further comprise any of those characters plus:

a number
a combining diacritical (accent) character
various joiner punctuation and zero-width spaces

These characters are defined in terms of Unicode character classes, so [A-Z] is incomplete. Ä is a letter; ξ is a letter; is a letter. You can use all of those in identifiers including those used for function names.

Unfortunately, JavaScript RegExp is not Unicode-aware. If you say \w you only get the ASCII alphanumerics. There is no feasible way to check the validity of non-ASCII identifier characters short of carrying around the relevant parts of the Unicode Character Database with your script, which would be very large and clumsy.

You could try simply allowing all non-ASCII characters, for example:

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

[EDIT] See @bobince's post below for a more correct and thorough answer. This answer has been retained for reference and edited to be less wrong.

A valid name in JavaScript must start with a Unicode letter (\p{L}), dollar sign, or underscore then can contain any of those characters as well as numbers, combining diacritical (accent) characters, and various joiner punctuation and zero-width spaces. Additionally, it cannot be a word reserved by the JavaScript language (e.g. abstract, as, boolean, break, byte, case, etc).

A full regular expression solution would be quite complicated in plain JavaScript but the XRegExp Unicode plugin could greatly simplify the task. This online function name tester might also be useful.

[ORIGINAL] Here is an incomplete regular expression, using only the US ASCII letters:

var fnNameRegex = /^[$A-Z_][0-9A-Z_$]*$/i;

You also must check that it doesn't match any reserved words (e.g. abstract, boolean, break, byte, ..., while, with, etc). Here's a start for that list, and an example function:

var isValidFunctionName = function() {
  var validName = /^[$A-Z_][0-9A-Z_$]*$/i;
  var reserved = {
    'abstract':true,
    'boolean':true,
    // ...
    'with':true
  };
  return function(s) {
    // Ensure a valid name and not reserved.
    return validName.test(s) && !reserved[s];
  };
}();

What you wan't is close to, or perhaps, impossible -- I haven't analyzed the grammar to know for sure which.

First, take a look at the ECMAScript grammar for identifiers. You can see one on the ANTLR site. Scroll down to where it defines identifiers:

identifierName:
    // snip full comment
    identifierStart (identifierPart)*
    ;

identifierStart:
    unicodeLetter
    | DOLLAR
    | UNDERSCORE
    | unicodeEscapeSequence
    ;

The grammar uses an EBNF, so you'll need follow those two non-terminals: identifierStart and identifierPart. The main problem you'll run into is that you need to take into account much of unicode, and its escape characters.

For example, with identifierStart, we see that the regular expression will need to allow a letter, a dollar sign, an underscore, or a Unicode escape sequence as the first 'character'.

Thus, you could start your regular expression:

"[$_a-zA-Z]..."

Of course, you'll need to change a-zA-Z to support all of Unicode and then augment the expression to support the Unicode Escape Sequence, but hopefully that gives you a start on the process.

Of course, if you only need a rough approximation, many of the other responses provide a rough regular expression that handles a small subset of what's actually allowed.

This should be very easy. Valid function names can only consist of alphanumerics, parenthesis, and possibly parameter values within the parens (i don't know enough javascript to know whether parameters are defined in the function call) and must start with a letter, correct? Therefore to validate that a string is a valid function name. Therefore this should work:

[a-zA-z]+[a-zA-z0-9_]\*(\\(.*?\\))\*
Related