I'd like to quickly check if a string is valid to be used as a property name using the dot notation rules (any letters or numbers as well as _
and $
as long as it doesn't start with a number) as obviously if bracket notation is used then everything is valid.
I've been trying to figure out a regEx solution but my knowledge of regEx is not great. I think that my current pattern will allow letters, numbers, $
and _
but I don't know how to disallow starting with a number
function validName(str){
// check if str meets the requirements
return /^[a-zA-Z0-9$_]+$/.test(str);
}
validName("newName") // should return TRUE
validName("newName32") // should return TRUE
validName("_newName") // should return TRUE
validName("4newName") // should return FALSE
validName("new Name") // should return FALSE
validName("") // should return FALSE