var obj = '{'include':true,'fn':'1-12'}'
I want to read fn value. how to remove single quotes which are present outside the {}?
var obj = '{'include':true,'fn':'1-12'}'
I want to read fn value. how to remove single quotes which are present outside the {}?
try this
remove double quotes from a String
like this:
var str = 'remove "foo" delimiting double quotes';
console.log(str.replace(/"([^"]+(?="))"/g, '$1'));
//logs remove foo delimiting quotes
str = 'remove only "foo" delimiting "';//note trailing " at the end
console.log(str.replace(/"([^"]+(?="))"/g, '$1'));
//logs remove only foo delimiting "<-- trailing double quote is not removed
Replace single quotes '
with double quotes "
and then use JSON.parse()
method to get a JSON object. The following works:
//var obj = '{'include':true,'fn':'1-12'}'; -- This assignment is invalid
var obj = "{'include':true,'fn':'1-12'}"; //Assuming double quotes outside
var obj1 = obj.replace(/'/g, "\""); //Replace single quotes with double quotes
console.log(typeof obj1); // string
var myjsonobj = JSON.parse(obj1); //convert to JSON
//myjsonobj is now a proper JSON object
console.log(myjsonobj); // Object {include: true, fn: "1-12"}
console.log(typeof myjsonobj); // object
console.log(myjsonobj.fn); // 1-12
Here it works for you
var regex = /[\']/g;
var obj = "'{'include':true,'fn':'1-12'}'";
obj = obj.replace(regex,"");
console.log(obj);
First of all this isn't valid JSON or javascript object
var obj = '{'include':true,'fn':'1-12'}'
Fix your object, then you can use eval()
like this:
var obj = JSON.parse('{"include":true,"fn":"1-12"}');
var fn = eval(obj.fn);