user3529166
user3529166

Reputation: 29

JS: Replace quotes between an specific symbol

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

Answers (2)

user663031
user663031

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

rock321987
rock321987

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)

Ideone Demo

Upvotes: 0

Related Questions