You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JSON Schema中转义正则表达式还原及匹配验证问题求助

正则表达式转义还原问题及解决方法

我有一个函数,接收正则表达式字符串和值,用于验证值是否匹配该正则。该函数在JSON Schema中调用,因此必须转义正则以避免JSON语法错误,但目前无法在函数中正确去除转义还原正则,导致验证失效。

JSON Schema 示例

{
  "type": "patientIdentifier",
  "label": "Recency Id",
  "questionInfo": "",
  "id": "recencyId",
  "required": "false",
  "default": "",
  "questionOptions": {
    "rendering": "text",
    "identifierType": "fd52829a-75d2-4732-8e43-4bff8e5b4f1a"
  },
  "validators": [{
    "type": "js_expression",
    "failsWhenExpression": "doesNotMatchExpression(\"/^REC\\d{5}-\\d{5,6}$/\", recencyId)",
    "message": "Please provide the correct format for the recency id ie. REC+mflCode+number eg.REC11902-00062"
  }]
}

当前函数代码

function doesNotMatchExpression(regexString, val) {
  console.log("val", val);
  console.log("regexstring", regexString);
  if (!val || ['undefined', 'null', ''].includes(val.toString())) {
    return true;
  }
  const cleanedRegexString = eval(regexString);
  const regex = new RegExp(cleanedRegexString);
  console.log("---h", regex.test(val));
  console.log("---p", regex);
  if (!regex.test(val)) {
    return true;
  }
  return false;
}

尝试过使用eval和regexString.replace(/^\/|\/$/g, '')处理,但正则中的反斜杠仍被移除,无法还原为原始正则规则。


问题根源

JSON解析时会自动处理转义字符,所以传入函数的regexString中,原本的\\d会被解析为\d。而使用new RegExp()时,字符串中的单个反斜杠会被视为转义符,导致\d被错误解析为普通字符d,最终正则失效。

解决方案

方案1:直接用eval获取正则对象

eval(regexString)会直接把带/的正则字面量字符串转换成正则对象,不需要再用new RegExp包裹,修改后的函数:

function doesNotMatchExpression(regexString, val) {
  console.log("val", val);
  console.log("regexstring", regexString);
  if (!val || ['undefined', 'null', ''].includes(val.toString())) {
    return true;
  }
  // 直接通过eval得到正则对象
  const regex = eval(regexString);
  console.log("---h", regex.test(val));
  console.log("---p", regex);
  // 简化返回逻辑
  return !regex.test(val);
}

方案2:手动处理转义字符(避免eval)

如果担心eval的安全风险,可以先去掉正则字符串前后的/,再把单个反斜杠替换成双反斜杠,确保new RegExp能正确识别正则元字符:

function doesNotMatchExpression(regexString, val) {
  console.log("val", val);
  console.log("regexstring", regexString);
  if (!val || ['undefined', 'null', ''].includes(val.toString())) {
    return true;
  }
  // 去除前后的/,并将单个\替换为\\
  const pattern = regexString.replace(/^\/|\/$/g, '').replace(/\\/g, '\\\\');
  const regex = new RegExp(pattern);
  console.log("---h", regex.test(val));
  console.log("---p", regex);
  return !regex.test(val);
}

方案3:修改JSON Schema传参格式

直接传递正则的模式字符串(不带前后的/),这样函数无需额外处理转义:
修改JSON Schema中的failsWhenExpression:

doesNotMatchExpression("^REC\\d{5}-\\d{5,6}$", recencyId)

对应的函数代码:

function doesNotMatchExpression(regexString, val) {
  console.log("val", val);
  console.log("regexstring", regexString);
  if (!val || ['undefined', 'null', ''].includes(val.toString())) {
    return true;
  }
  const regex = new RegExp(regexString);
  console.log("---h", regex.test(val));
  console.log("---p", regex);
  return !regex.test(val);
}

内容的提问来源于stack exchange,提问作者CKW

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 03:38:17