Created
August 19, 2016 20:20
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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