【发布时间】:2021-10-15 05:48:53
【问题描述】:
无论我尝试什么,比如使用 \\w+ 甚至 \\\\w+ 转义 \w+,我都无法让 gatsby/graphql 理解我的查询。或者看起来是这样。很多时候,我可以完全按照我的意愿在页面上看到我想要呈现的确切数据,但是 gatsby 的错误消息覆盖告诉我 graphql 在\w 周围遇到了无效的转义序列,这掩盖了它。这是我的代码:
文件结构:
site/
-src/
--content/
---adventures/
----example/
-----index.mdx
-----etc...
----doip/
-----index.mdx
-----etc...
----lomp/
-----index.mdx
-----etc...
想要的结果:
我有三个带有.mdx 子页面的目录,我想在/src/pages/adventures.js 上列出。我只想获取上面列出的索引。 每个冒险目录中还有许多其他目录以及其他 .mdx 文件,因此任何没有某种过滤器的 graphql 查询都会提供数十个结果,而我只想要三个结果。我想要/adventures/\w+/$/i,但没有任何效果。
我的查询:
在我尝试解决此问题时,我的查询采用了多种形式,但按原样,这不起作用。 "/adventures/\\\\w+/$/i" 或 "/adventures\/\w+\/$/i" 或我尝试或读过的任何变体也没有。我写在adventures.js 文件上的查询开头为:
query AdventuresListPageQuery {
allMdx(filter: {fields: {slug: {regex: "/adventures\\\\/\\\\w+\\\\/$/i"}}}) {
edges {
node {
id
frontmatter {
title
setting
levels
playernum
}
fields {
slug
}
body
}
}
}
}
请帮我制作一个只获取我需要的文件的查询:(
【问题讨论】:
-
试过
regex: /adventures\/\w+\/$/i?不带引号。 -
刚试过。收到此错误:
GraphQLError: /Users/g/Sites/dmcm/src/pages/adventures.js: Syntax Error: Cannot parse the unexpected character "/". -
如果正则表达式有问题,请尝试构造函数:
regex: new RegExp('adventures/\\w+/$','i')或regex: new RegExp('adventures/[^/]+/$','i') -
这给了我
Syntax Error: Expected ":", found "(".
标签: reactjs regex graphql escaping gatsby