baseframe-js
Version:
A suite of useful Javascript plugins and functions to help with Front-end Development on websites
14 lines (13 loc) • 549 B
JavaScript
const parseObjectFromString = (options) => {
let retObj = null;
if (typeof options === 'string') {
retObj = JSON.parse(options.replace(/:\s*"([^"]*)"/g, function (match, p1) {
return ': "' + p1.replace(/:/g, '@colon@') + '"';
}).replace(/:\s*'([^']*)'/g, function (match, p1) {
return ': "' + p1.replace(/:/g, '@colon@') + '"';
}).replace(/(['"])?([a-z0-9A-Z_]+)(['"])?\s*:/g, '"$2": ')
.replace(/@colon@/g, ':'));
}
return retObj;
};
export default parseObjectFromString;