Gatsby: Invalid character escape sequence in GraphQL regex filter

Created on 15 Nov 2018  Â·  3Comments  Â·  Source: gatsbyjs/gatsby

Hi,

I am currently using Gatsby to build a documentation site for a HTML/CSS component based design system. I have installed the transformer-remark plugin as I am populating content on each Component page using the README.md files for each component.

On the left hand side of the page is a dynamic sidebar which lists all the components so a user can navigate between each component’s page.

I am using some Graphql aliased queries to retrieve a list of the components and they are grouped according to a regex filter of each node’s fileAbsolutePath. However I am getting the following error which is preventing React from compiling the site:

Syntax Error: Invalid character escape sequence: \w.

The filter within my query looks like so:

fileAbsolutePath: {regex: “/dl-atoms-(\w+)/README/}

I have tried escaping the backslash before the w by adding a further backslash but I just get another very similar error.

Is there a special way of escaping reg ex required in GraphQL?

question or discussion

Most helpful comment

Backlashes need to be escaped twice, first because you provide a String not a RegExp literal, and second for the GraphQL parser. It's not pretty, but /\w+/ becomes "/\\\\w+/".

All 3 comments

@decodedcreative I think the regex you provided is invalid. It should be like this:
/dl-atoms-(\w+)\/README/

Backlashes need to be escaped twice, first because you provide a String not a RegExp literal, and second for the GraphQL parser. It's not pretty, but /\w+/ becomes "/\\\\w+/".

Fixed in #11002

Was this page helpful?
0 / 5 - 0 ratings