Variables in regexp with JavaScript client

I’m having trouble using variables in a regexp() query with the JavaScript client. Here’s the query in Ratel, with variables, working perfectly:

And here’s my code, which is finding no results:

const name = 'ife'
const limit = 2

const query = `
  query tags($regex: string, $limit: int) {
    tags(func: regexp(Tag.name, $regex), first: $limit) {
      expand(_all_)
      uid
    }
  }
`

const response: Response = await txn.queryWithVars(query, { $regex: `/.*${name}.*/i`, $limit: `${limit}` })
return response.getJson()?.tags

Am I setting the variables incorrectly or something? I did see this post about a very similar issue (seems like it was never resolved), as well as this issue related to regex variables not working with the Go client. Is it possible that the JavaScript client has a similar problem with regex variables?

The regexp is wrong. You’re using Javascript flavoured regular expressions. Dgraph supports proper regular expressions.

There shouldn’t be / in the regex. When you are using Ratel, / serves as a delimiter for the regular expression, much like " serves as a delimiter for strings. You’re passing in the regular expression as a string when calling via JS, not as a regular expression (object).

To construct your regex string, the following JS would work:

`.*${name}.*`

p/s: Don’t write .*. It’s a bad habit. Don’t write .+ either. Also a bad habit.
pp/s: proper regular expression doesn’t have name interpolation/variable or named capture. Those are called PCRE (Perl Compatible Regular Expressions), which are NOT Regular Expressions. I don’t think they’re even regular. Go only supports regular expressions.
ppp/s: yes, I am a stickler for formal language hierarchies.

Thanks for the response.

You’re using Javascript flavoured regular expressions.

Yes, and the argument could be made that JavaScript regular expressions should be supported when one is using the official Dgraph JavaScript client. Nevertheless, I’ll switch to what Dgraph supports, but this doesn’t help me do that:

Dgraph supports proper regular expressions.

Could you please link me to something that documents the proper regular expressions supported by Dgraph so I don’t use the wrong syntax?

In particular, I’m wondering how to ensure that my match is case insensitive. In JavaScript, I would use the i flag after the closing / to achieve this (which works in Ratel):

/pattern/i

I was able to get this resolved, but it looks like the / characters are required. If I remove them, I get an error from Dgraph:

Error: 2 UNKNOWN: Unexpected error while parsing regex arg: ife
  at Object.callErrorFromStatus (node_modules/@grpc/grpc-js/src/call.ts:81:24)
  at Object.onReceiveStatus (node_modules/@grpc/grpc-js/src/client.ts:334:36)
  at Object.onReceiveStatus (node_modules/@grpc/grpc-js/src/client-interceptors.ts:434:34)
  at Object.onReceiveStatus (node_modules/@grpc/grpc-js/src/client-interceptors.ts:397:48)

Hmm, then I was wrong.

But either way the syntax supported by Dgraph is this (proper RE doesn’t do backtracking):

The package we use internally is the built in Go package:

For more information, please refer to

This view of course is not the majority or plural view. The more widely accepted view is one espoused by Damian Conway:

1 Like

Thanks again for your response. This is very informative.

WHAT!?!

Executing a regular expression is like traversing a graph