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

如何编写RegEx提取URL中的工单ID 解决匹配返回Null问题

错误原因

  • 第一种写法的/\d/仅能匹配单个数字,且String.match()返回值为匹配结果数组,未定向捕获工单ID时无法拿到有效值,匹配失败就会返回null,拼接后就得到你看到的错误结果。
  • 第二种写的正则是通用URL格式校验规则,没有专门针对工单ID的捕获分组,同样无法提取到目标值。

正确实现

你的示例URL中工单ID固定在/tickets/路径之后的数字段,直接用正则分组捕获该段内容即可:

const input = document.getElementById('nameInput')
// 捕获工单ID
const ticketMatchRes = window.location.href.match(/\/tickets\/(\d+)/)
const ticketId = ticketMatchRes ? ticketMatchRes[1] : 'Unknown'
const data = {
  "name": input.value || `Problem with ticket ${ticketId}`,
  "story_type" : "Bug",
  "description": window.location.href
}

说明

正则/\/tickets\/(\d+)/会定向匹配路径中/tickets/后的连续数字串,适配Zendesk标准工单URL格式。如果页面URL不符合工单路径规则,会默认填充Unknown,避免出现null拼接的异常。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 06:48:01