0

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,......);
Ole Albers
  • 7,645
  • 7
  • 56
  • 130
  • 2
    You probably shouldn't apply the regex to the entire json string; parse the json, find the variable you want and only then apply the regex to the actual string; let your json serializer do the rest. – beerbajay Feb 23 '14 at 11:10
  • I did. It was just about the "short" property – Ole Albers Feb 23 '14 at 11:11
  • possible duplicate of [Escape string for use in Javascript regex](http://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex) – Dean Taylor Feb 23 '14 at 11:19
  • @DeanTaylor No. It was not "Just" about escaping a regex but the combination regex/json ("double-escaping") – Ole Albers Feb 23 '14 at 11:21
  • 1
    @OleAlbers it's a string, it doesn't matter that it is JSON, other than the input to the escaping is a JSON string, ultimately just a string to escape into the RegEx. – Dean Taylor Feb 23 '14 at 11:24
  • @DeanTaylor I added the code, which should make it clearer. (But is solved nonetheless) – Ole Albers Feb 23 '14 at 11:57
  • If you solved it, you should answer your own question, and accept it. – zord Feb 23 '14 at 12:20
  • I hate answering myself, looks like clapping on my own shoulder... :) But well. did it – Ole Albers Feb 23 '14 at 12:27

1 Answers1

1

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

Ole Albers
  • 7,645
  • 7
  • 56
  • 130