Wednesday 15 January 2014

javascript regex replace all doublequotes in string unless the doublequote is followed by space or comma space -


I have an issue where a person is not leaving dual quotes correctly in values, later it is a JSON string Can be explained as.

Example of the string:

  {"description": "This is my 12" pizza order. "," Value ":" 1 "}   

When I try to handle JSON.parse (), then it's an incomplete double quote (in reference to the size in inches) The reason is error

First of all, I thought - just do:

  string.replace (/ \ "/ g, '\"');   

But of course it's all valid double quotes, I'm not an expert on regex, but I looked around for some answers and I think it needs a negative attitude?

Anyone Sequence of DoubleCloth can help to create a regex to find (replacement) to find C, where the next 2-letter sequence is not space-comma after criminal double-click?

I know that There is no universal decision, (to correct the person at its own end), but unfortunately I do not have the luxury of a universal decision.

TIA

Update - Example string Instead of considering (only chit Is it possible for the presence of valid JSON before and after each Doubletotot - i.e. before and after each double quote, any of the following letters, <<: < P> to see? I think this is what I was asking - can it be done with viewing / regex behind?

The best I can do is taking advantage of the fact that an unqualified quote in JSON only Can be in some places only.

  input = '{"description": "This is my 12" pizza, and I call "thin crust", "value": "1"}; Console.log (input); Output = input Replace (/ "," / g, '_COLON_') replace (/ "," / g, '_COMMA _'). (/ /, '_OPEN_'). "} / G, '_CLOSE_'); Output = output.rele (/ "/ g, '\\' '); output = output. Location (/ _OPEN_ / g,' {" ''). Replace (/ _ COLON_ / g, '":"' '). Replace (/ _ COMMA_ / g, '","'). Replace (/ _CLOSE_) / "description": "This is my 12" pizza, and "thin crust", "value": "1"} {"description": "this is my 12 \" pizza, and I think You can change the 'Open', 'Close', etc., from which the "Input" The probability might be low, perhaps it is also possible troll characters if you do not pay attention to the ingenious reggae, but as mentioned by other people, there is no solution in all cases Will happen. No matter what you do, it is the same value that can be in the description text that will disturb you, because unlike the properly generated JSON, the syntax you are trying to parse is unclear.   

No comments:

Post a Comment