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\\&g
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.