How to find special characters that are inside other characters using javascript

Asked

Viewed 1,455 times

3

I have a string value in json format '{"text": "Look "I" here"}' and I want to change this quote "I" because it will give error in the code. I was thinking of using JSON.parse and loop the items, only the JSON function does not run because of the error, because it cannot use the same quote type as the beginning and end within the string other than to mention a Function or var.

I think we can do this by using replace to exchange the quotation marks inside the quotation marks.

  • 4

    Where does this string come from?

  • 1

    Where this Json is being formatted, I agree with @brasofilo, the error ta ai ?

  • This is my way of saving the settings, using string-like json in localStorage in an application of mine. I’m afraid the user put a quote and give error in the code

  • The previous version of my app did not prevent it, but there was no error, now with the migration I will do it may be that in the settings have this quote

  • 1

    Classic XY problem :D

2 answers

3

As Brasofilo said in the comments, the ideal is to prevent this wrong string from entering JSON first. Dealing with the problem after the fact is much harder...

If your JSON has exactly this format, just take everything inside the string and make a replace:

var string = '{"text": "Olha "eu" aqui"}';
var prefixo = '{"text": "';
var sufixo = '"}';

var conteudo = string.substring(prefixo.length, string.length-sufixo.length);

var novaString = prefixo + conteudo.replace(/"/g, '\\"') + sufixo;

If not, you are in trouble... How to interpret the string below?

{"text":"b","c":"d"}
  1. Key: text, value: b; key: c, value d? or:
  2. Key: text, value: b","c":"d?

That is: the only solution correct is to address the problem before the string ends up in JSON. If you have for example a legacy file in which - for any bug - the format got wrong this way, you can even use an automated process to help you fix, but you have to review it by hand... Now, if it is an existing code that is generating this type of string, this code is bug and you should fix it in the source - and not apply a "band-aid"...

  • Vish =/ I thought it would be easier and that with the example you were going to make I could adapt. The json I use has many more things, like: '{"config": [{"text": "...", "type": "text"}, {"text": "...", "type": "text"}]}

  • @And you can’t remove the quotes before to put the string in JSON? By the way, you shouldn’t "mount" JSON by hand: create an object, then use JSON.stringify in it. If there are any quotes in the strings it "escapes" for you.

1

If you want to change all "eu" it’s easy:

var string = '{"text": "Olha "eu" aqui"}';
var stringLimpa = string.replace('"eu"', 'eu');

If you want to change all content of text then you can test this:

var string = '{"text": "Olha "eu" aqui"}';
var conteudo = string.match(/{"text": "(.*)"}/)[1]; // criar um string com o conteudo
string = string.replace(conteudo, conteudo.replace(/"/g, '\'')); // subtituir o conteudo por novo conteudo com ' em vez de "
console.log(string); // {"text": "Olha 'eu' aqui"}  // só para confirmar

var json = JSON.parse(string);
console.log(json); // Object {text: "Olha 'eu' aqui"} 

Example


Simplifying, and putting into a function:

function limpar(s) {
    var conteudo = s.match(/{"text": "(.*)"}/)[1];
    var conteudoLimpo = conteudo.replace(/"/g, '\'');
    s = s.replace(conteudo, conteudoLimpo);
    return JSON.parse(s);
};

var string = '{"text": "Olha "eu" aqui"}';
var objeto = limpar(string);

Example

Browser other questions tagged

You are not signed in. Login or sign up in order to post.