If the variables are in the global scope (that is, they are not created inside the function), they should also be available as properties of the window object. In other words, the opts_1234 variable can also be obtained as window.opts_1234 or window['opts_1234'] .
The easiest way to capture all variables:
var variables = Object.keys(window).filter(function(prop) { return prop.substring(0, 5) === 'opts_'; });
variables now contains an array of names, for example ['opts_1', 'opts_666']
You can also expand this filter to include only those variables that are not undefined:
var variables = Object.keys(window).filter(function(prop) { return prop.substring(0, 5) === 'opts_' && window[prop] !== undefined; });
source share