Modify your regexp as below. But I don't know javascript regexp mechanism. I believe someone explain it.
before:
var ticketPattern = new RegExp('/^('+projectId+'-*\d)$', 'g');
after:
var ticketPattern = new RegExp('^('+projectId+'-*\\d*)');
Question
I am trying to get a Ticket IDs in a log message to be wrapped with a link to the appropiate URL in Javascript. I am still relatively a newbie when it comes to regex.
Input:
MYPROJECT-123 My Commit
Output:
(MYPROJECT-123)[http://url/MYPROJECT-123] My Commit
I am scratching my head in figuring out why my regex pattern will not work. Any clues?
var logMessage = "MYPROJECT-123 My Commit";
var projectId = "MYPROJECT";
var ticketPattern = new RegExp('/^('+projectId+'-*\d)$', 'g');
var mdLogMessage = logMessage.replace(ticketPattern, "($1)[http://url/$1]");
console.log(mdLogMessage);
Solution
Modify your regexp as below. But I don't know javascript regexp mechanism. I believe someone explain it.
before:
var ticketPattern = new RegExp('/^('+projectId+'-*\d)$', 'g');
after:
var ticketPattern = new RegExp('^('+projectId+'-*\\d*)');
OTHER TIPS
Try it like this:
var ticketPattern = new RegExp('/'+projectId+'(?=-\d*\s*My Commit)', 'g')
so the regex pattern will be for example:
MYPROJECT(?=-\d*\s*My Commit)
see demo with 'MYPROJECT' as the projectId here: http://regex101.com/r/lM7rJ3