Remove Quotes in JavaScript

Removing quotes from returned CSV file

(cols[0]).replace(/"/g, "") before years.push(cols[0]) statement will do, if you are only having problem with double quotes.

remove outer double quotes from array of strings

The real solution is fix whatever is producing invalid JSON and make it valid.

If you can not fix it, you can manipulate the data as long as you can say that the content will not have any quotes, a simple replace can fix it.

var x = "['a', 'b', 'c']";
var obj = JSON.parse(x.replace(/'/g,'"'));
console.log(obj);

remove quotes around ONLY numeric values in string

I'd parse the JSON and use a reviver function to replace digit strings with just the digits:

var arr= '[{"a":"b","c":"521"}]';
const parsed = JSON.parse(
arr,
(key, value) => typeof value === 'string' && /^\d+$/.test(value)
? Number(value)
: value
);
console.log(JSON.stringify(parsed));

Remove white space inside single quotes before and after text inside the quotes

Lookahead for exactly one ' before the end of the string:

const input = `testName='   test   '`;
const cleaned = input.replace(/ +(?=[^']*'$)/g, '');
console.log(cleaned);

Removing double-quotes around Keys in Array

When you write JSON.stringify to convert it in the string, it would add the double quotes to your keys.

You just have to write obj directly, it would behave as the normal object and not string.

So please update your code of lines with the below code. It should resolve your problem.

var obj = {};
obj.Firstname = document.getElementById("firstName").value;
obj.Lastname = document.getElementById("surname").value;
var jsonStringObj = {users: [obj]};
console.log(jsonStringObj)


Related Topics



Leave a reply



Submit