1
var obj = '{'include':true,'fn':'1-12'}'

I want to read fn value. how to remove single quotes which are present outside the {}?

Tanvi Shah
  • 497
  • 2
  • 9
  • 18
  • JSON.parse will not work. It expects double quotes. These look like single quotes. – Rajesh Jan 12 '16 at 10:32
  • 8
    First of all, this is not a valid string. – Rayon Jan 12 '16 at 10:34
  • 1
    I don't see double quotes – dnit13 Jan 12 '16 at 10:35
  • 1
    @OP, Where did the `obj` came from ? Did you try to make this on your own ? – Rayon Jan 12 '16 at 10:37
  • You can check following [SO - post](http://stackoverflow.com/questions/34650511/convert-array-to-objects-for-jsondata-objects-in-js/34650757?noredirect=1#comment57047834_34650757) for reference. – Rajesh Jan 12 '16 at 10:37
  • Object is a string? ```var x = eval("(" + obj + ")"); console.log(x.fn)``` – tenbits Jan 12 '16 at 10:42
  • @SidneyLiebrand, I think it a copy past problem... the quotes are probably escaped in original code ) – tenbits Jan 12 '16 at 10:44
  • @tenbits removed my upper comment as I thought I was wrong there for a second but when running this line (the equivelant) I get a parse error myself: `var x = eval("('{'include':true,'fn':'1-12'}')"); console.log(x.fn)` so if the input is escaped as well it's not even close ;) – SidOfc Jan 12 '16 at 10:46
  • Why you need to remove quotes? You want to get the fn value rt? – Sanjay Kumar N S Jan 12 '16 at 10:46
  • 2
    @SidneyLiebrand, I mean, try in devconsole to write ```var str = "a\"b"```, then print str, press enter - you will see ```"a"b"```, so I think the code is copied from some similar place. – tenbits Jan 12 '16 at 10:52
  • 2
    @SidneyLiebrand, and you tried it not correct - it should be ```var x = eval("({'include':true,'fn':'1-12'})"); ```. ``obj`` is of type string. And the start-end quotes are not the part of the string – tenbits Jan 12 '16 at 10:56
  • @tenbits oh dayum! That works really well and indeed I entered it wrong - my bad :S - Only one thing remains now which would be if the OP is receiving input from users or not which will define wether or not `eval` is safe to use, otherwise I think it's a good answer (and shouldn't be a comment instead of the other way around :P) – SidOfc Jan 12 '16 at 13:34

4 Answers4

0

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
Community
  • 1
  • 1
23nikoloz
  • 70
  • 6
0

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
Beat
  • 4,386
  • 2
  • 35
  • 52
jim1427
  • 140
  • 2
  • 9
-1

Here it works for you

var regex = /[\']/g;
var obj = "'{'include':true,'fn':'1-12'}'";
obj = obj.replace(regex,"");
console.log(obj);
Wasiq Muhammad
  • 3,080
  • 3
  • 16
  • 29
-2

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);
Ruben Yeghikyan
  • 497
  • 2
  • 6
  • 19