Ole Albers
Ole Albers

Reputation: 9285

Escaping JSON and Regex

I need a json-string to be used in a regex. Honestly I am a bit confused about all the escaping.. :)

I want to replace "\o/" by something else by regex. The source string should be in a JSON.

This is the JSON-String without escaping:

var emoticons=...
    + '{"img": "party.png","short": "\o/"},'
...

I tried to escape the slash and backslash to meet the regex' need but after that wasn't able to parse the JSON anymore:

var emoticons=...
    + '{"img": "party.png","short": "\\o\/"},'
...

How should the JSON-String look like?

Code:

var smilies = JSON.parse(emoticons);
text.replace(new RegExp(smilies.short,......);

Upvotes: 0

Views: 1227

Answers (1)

Ole Albers
Ole Albers

Reputation: 9285

Solved: The string would be "\\\\o\\/"

Upvotes: 1

Related Questions