首页 > 解决方案 > 用于在 URL 中捕获票证 ID 的正则表达式

问题描述

我正在尝试制作一个正则表达式,它将隔离我的 URL 中的票证 ID 号,并在错误报告中将它们发回给我,如下所示。

  const input = document.getElementById('nameInput')
        data = {
          "name": input.value || "Problem with ticket"+ window.location.href.match(/\d/),
          "story_type" : "Bug",
          "description": window.location.href

我也试过拉整个网址

 "Problem with ticket"+ window.location.href.match(/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/)

但是,两者都返回“票空问题”,我承认我在正则表达式方面很糟糕,那么写这个的最好方法是什么?我在下面附上了一个示例 URL。

https://xoxoxox.zendesk.com/agent/tickets/2116248

标签: javascriptregex

解决方案


如果票证只有数字。

"https://xoxoxox.zendesk.com/agent/tickets/2116248".replace(/.*\/(\d*)/,"$1")

结果:2116248


推荐阅读