Reputation: 29
Let's say I have the string:
"Audio":[{"ID":"0","Codec": "AC-3","Info":"192 Kbps, 48.0 KHz (2 channels)","Language":"Spanish (##He aqui "el teléfono que nunca suena" pero debería##)","Default":"Yes"}]
And I want to convert into:
"Audio":[{"ID":"0","Codec": "AC-3","Info":"192 Kbps, 48.0 KHz (2 channels)","Language":"Spanish (He aqui \"el teléfono que nunca suena\" pero debería)","Default":"Yes"}]
As you can see, I want to escape all the quotes that exists between the "##" symbols, addtionally remove that symbols too, how can I achieve this by using the replace()
method? I mean how could be the regex pattern. Thanks
Upvotes: 0
Views: 97
Reputation:
If you're intent on doing this, then
input.replace(/##(.*?)##/g, function(match) { return match.replace(/"/g, '\\"'); })
should do the job.
This replaces all the ##...##
sequences with the result of replacing quotes inside them with the escaped quote.
Upvotes: 1
Reputation: 11032
var str = '"Audio":[{"ID":"0","Codec": "AC-3","Info":"192 Kbps, 48.0 KHz (2 channels)","L##a"g##uage":"Spanish (##He aqui "el teléfono que nunca suena" pero debería##)","Default":"Yes"}]';
var result = str.replace(/##(.*?)##/g, function(v) { return v.replace(/"/g, "\\\""); });
print(result)
Upvotes: 0