Yes, you can try to access the property of an object using a regular expression, but no, it wonβt do what you want: it converts the regular expression to a string and uses that property name.
The only way to find the name of a property in an object by matching with a regular expression is for ... in loop, as you mentioned. Performance should not be a problem if an object has only one property.
function findPropertyNameByRegex(o, r) { for (var key in o) { if (key.match(r)) { return key; } } return undefined; }; findPropertyNameByRegex(obj, regxp);
source share