Skip to content

Instantly share code, notes, and snippets.

@sethbergman
Created August 19, 2016 20:20
Show Gist options
  • Save sethbergman/5052bde05f8a5148bf285a901b4079e0 to your computer and use it in GitHub Desktop.
Save sethbergman/5052bde05f8a5148bf285a901b4079e0 to your computer and use it in GitHub Desktop.
Function that tests whether a string can be used as a variable name
function isValidVarName(varName) {
try {
Function(varName.replace(/[\s\xA0,\/]|^$/g, '.'), '');
return true;
}
catch (e) {
return false;
}
}
function test(str) {
console.log({ varName: str, isValid: isValidVarName(str) });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment