I Love Python
I Love Python

Reputation: 872

javascript: how to trim excessive backslashes in JSON?

I have crazy amount of backslashes in some of my JSON strings.

example:

{"xpath":"//*[@id=\\\\\\\\"pagination_contents\\\\\\\\\\\\"]/div[3]/div/div/form/div[2]/a","title":"keyword"}

I would like to shave anything more than \" so \\\\\\\\" would be \". Any amount of \\\\ would be trimmed.

I tried

str.replace("\\\"","\"") and not sure how to account for varying amount of backslashes.

Upvotes: 1

Views: 32

Answers (1)

Sabaz
Sabaz

Reputation: 5282

.replace(/\\\\+/g, "\\") should do the trick

for example "abc\\\\\\\\\\\\\\\\def\\\\\\\ghi".replace(/\\\\+/g, "\\") will return "abc\def\ghi"

Upvotes: 1

Related Questions