storing regex in json

5,427 views
Skip to first unread message

Stephen Bartell

unread,
Jan 18, 2013, 4:19:17 PM1/18/13
to us...@couchdb.apache.org
Hi,

I'm trying to do something similar to the couchdb view engine. I want to store javascript snippets in a couchdb document, and then use a process (node.js) to execute the snippets.

The problem is stringifying the snippets into json. What I currently do is just compress my js, then paste it into the document via futon's create new document page. This seems to work fine. Unless regex is involved, obviously because regex isn't a JSON primitive.

But futon serializes this somehow for the views. I've dug around the couchjs source code to see how the futon ui does this on the _temp_view page, but I must be braindead here. Im not finding it.

Could someone point me in the right direction?

Best,
sb

Jens Alfke

unread,
Jan 18, 2013, 4:58:40 PM1/18/13
to us...@couchdb.apache.org

On Jan 18, 2013, at 1:19 PM, Stephen Bartell <snba...@gmail.com> wrote:

> The problem is stringifying the snippets into json. What I currently do is just compress my js, then paste it into the document via futon's create new document page. This seems to work fine. Unless regex is involved, obviously because regex isn't a JSON primitive.

I don’t think this has anything to do with regexes. It shouldn’t matter what’s in your JS code (or even whether it’s JS or Erlang or FORTRAN) as long as you’ve properly quoted it. To the document it’ll just be a string.

How are you quoting the code?

—Jens

Stephen Bartell

unread,
Jan 18, 2013, 5:52:41 PM1/18/13
to us...@couchdb.apache.org
I use double quotes for the string as a whole, and then single quotes for quotes in the string.

Example:
{
"map": "function (a, b) {var res = b.match(/(\d+)/); console.log('this is the res:' + res)}"
}

This shows I use regex escapes, and it shows how I do quotes (in the console.log fund).

This won't store properly via futon. Meaning, if I put this into a document via futon, the value will become a string, not a json object.

I think it might come down to the regex escape "\d+". If i use "d+" instead, then futon will accept the object. But then running the snippet won't work because i intend to have "\d+".

>
> —Jens

Mark Hahn

unread,
Jan 18, 2013, 5:57:36 PM1/18/13
to user
Have you tried \\ instead of \ ?

Jens Alfke

unread,
Jan 18, 2013, 6:09:58 PM1/18/13
to us...@couchdb.apache.org

On Jan 18, 2013, at 2:57 PM, Mark Hahn <ma...@hahnca.com> wrote:

> Have you tried \\ instead of \ ?

+1. When quoting text for JavaScript or JSON, you have to escape any backslashes in the text.

—Jens

Stephen Bartell

unread,
Jan 18, 2013, 6:20:36 PM1/18/13
to us...@couchdb.apache.org

>
> On Jan 18, 2013, at 2:57 PM, Mark Hahn <ma...@hahnca.com> wrote:
>
>> Have you tried \\ instead of \ ?
>
> +1. When quoting text for JavaScript or JSON, you have to escape any backslashes in the text.

I did. I verified my javascript func worked. Then I compressed it. Then i went through and replaced the \'s with \\'s. When the func was eval'd it would throw.

I'm going to run through the whole thing again to triple check though.

>
> —Jens

Stephen Bartell

unread,
Jan 18, 2013, 7:43:30 PM1/18/13
to us...@couchdb.apache.org
Like you guys said, escaping backslashes is how you put regex into json.

My problem was that in the real world, the data going into my function was not the type i expected. So my function kept throwing. It worked great in my cute little tests, but burned in production.

Thanks for the help,
sb
Reply all
Reply to author
Forward
0 new messages