I'm trying to write a regex that will replace all invalid characters in a JavaScript variable name with underscores (in Java).
What I'm wanting to do is:
String jsVarName = "1inva>idName".replaceAll("[a-zA-Z_$][0-9a-zA-Z_$]", "_");
and end up with a variable named _inva_idName
.
What I'm struggling to do is figure out how to make the first character different to the others.
[a-zA-Z_$][0-9a-zA-Z_$]
are the characters I want, but I cant figure out to hook them into the correct syntax. I know JS var names can be full unicode, but I only care about about ASCII.