Created
July 21, 2016 12:36
-
-
Save tejpratap46/ed2efe54a2b19e247f5f4ca85fa97b3c to your computer and use it in GitHub Desktop.
Check if string is valid as a variable. It is useful for selection username etc.
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 isVarName(str) { | |
'use strict'; | |
if (typeof str !== 'string') { | |
return false; | |
} | |
try { | |
new Function('var ' + str)(); | |
} catch (e) { | |
return false; | |
} | |
return true; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment