You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Stephen Bartell <sn...@gmail.com> on 2013/01/18 22:19:17 UTC

storing regex in json

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

Re: storing regex in json

Posted by Stephen Bartell <sn...@gmail.com>.
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

On Jan 18, 2013, at 3:09 PM, Jens Alfke <je...@couchbase.com> wrote:

> 
> 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


Re: storing regex in json

Posted by Stephen Bartell <sn...@gmail.com>.
> 
> 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


Re: storing regex in json

Posted by Jens Alfke <je...@couchbase.com>.
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

Re: storing regex in json

Posted by Mark Hahn <ma...@hahnca.com>.
Have you tried \\ instead of \ ?


On Fri, Jan 18, 2013 at 2:52 PM, Stephen Bartell <sn...@gmail.com>wrote:

>
>
> >
> > On Jan 18, 2013, at 1:19 PM, Stephen Bartell <sn...@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?
>
> 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
>
>

Re: storing regex in json

Posted by Stephen Bartell <sn...@gmail.com>.

> 
> On Jan 18, 2013, at 1:19 PM, Stephen Bartell <sn...@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?

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


Re: storing regex in json

Posted by Jens Alfke <je...@couchbase.com>.
On Jan 18, 2013, at 1:19 PM, Stephen Bartell <sn...@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