Home > OS >  RegEx for capturing ticket ID's in a URL
RegEx for capturing ticket ID's in a URL

Time:09-21

I'm attempting to craft a RegEx that will isolate the ticket ID numbers in my URL and send them back to me in a bug report as seen below.

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

I have also tried pulling the whole URL

 "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()@:%_\ .~#?&//=]*)/)

However, both return "Problem with ticket Null", I admit I'm terrible at RegEx, so what would be the best way to write this? I've attached an example URL below.

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

CodePudding user response:

If ticket ID is only digits.

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

Result : 2116248

CodePudding user response:

You can try the following RegEx \d for retrieving the required ID,

As demonstrated in the code snippet below,

url = "https://xoxoxox.zendesk.com/agent/tickets/2116248";

let ticketID = url.match(/\d /);
console.log(ticketID);

Read more about it here

CodePudding user response:

If there can be more digits in the url, you can make it specific to get it after tickets in a capture group.

Check first if there is a match, and then get the capture group 1 value:

\/tickets\/(\d )

Regex demo

let m = "https://xoxoxox.zendesk.com/agent/tickets/2116248".match(/\/tickets\/(\d )/);
if(m) {
  console.log(m[1]);
}

Output

2116248
  • Related