如何解决模板文字中的正则表达式转义字符
我正在尝试将mapql-constraint-directive与阿波罗graphql服务器一起使用。 我需要把reg。像这样的模板文字中的表达式
PHP://input
这对我不起作用。没有匹配。正则表达式是正确的。该示例使用此 @constraint(pattern:“ ^ [0-9a-zA-Z] $”)*。
所以我意识到我的模板文字typeDefs中的 ^ 05 \ d([-] {0,1})\ d {7} $ 出了问题。
我已经签入Node REPL:
const typeDefs = gql`
input ClaimInput {
id: String!
date: String!
phone: String @constraint(pattern: "^05\d([-]{0,1})\d{7}$")
email: String
invoice: String
doctorId: String
therapy: [TherapyInput]
files: [FileInput]
}
`
当我输入\ d \时,将被忽略。 当我放\\ d时,我有\\ d。不是我所需要的。
如何处理这种情况?
编辑:
我认为问题出在“
当我更改为:
> v = `^05\d([-]{0,1})\d{7}$`
'^05d([-]{0,1})d{7}$'
> v = `^05\\d([-]{0,1})\d{7}$`
'^05\\d([-]{0,1})d{7}$'
我有一个错误: GraphQLError:语法错误:无效的字符转义序列:\ d。
当我更改为
const typeDefs = gql`
input ClaimInput {
id: String!
date: String!
phone: String @constraint(pattern: "^05\\d([-]{0,1})\\d{7}$")
email: String
invoice: String
doctorId: String
therapy: [TherapyInput]
files: [FileInput]
}
`
我有同样的错误: GraphQLError:语法错误:无效的字符转义序列:\ d。
解决方法
您可以使用String.raw
:
v = String.raw`^05\d([-]{0,1})\d{7}$`
如果您不/不能使用\\d
,则 String.raw
也正确。由于REPL中的预览必须是可复制复制的字符串,因此它仅显示为\\d
。如果您console.log(v)
只会看到一个反斜杠。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。