Javascript regex- How to find PascalCase items inside of group

I’m needing to convert something like this:

[[Click here|ThisIsALink]]


[Click here](

I’m able to find the first [[Click here|ThisIsALink]] by using this regex /[[(.*?)|(.*?)]]/g which I can replace to turn it into [Click here](]), but how then can I take that last part and convert it to this-is-a-link? I’ve tried a few things but I seem to be finding all PascalCased occurrences and not just the occurrences that happen within the first found regex pattern. (I am converting .md files to .pdf, so it is a full page of content, not just these links).

Here’s what I have so far

var exampleUrl = "";
var urlOne = /[[(.*?)|(.*?)]]/g;

data.replace(urlOne, "[$1](" + exampleUrl + "$2)"));

This results in: [Click here](


You can use

const texts = ["Text [[Click here|ThisIsALink]] text...",
               "Text [[Click here|This-Is-A-Link]] text..."];
const exampleUrl = "";
const urlOne = /[[((?:(?![[).)*?)|(.*?)]]/g;
for (var text of texts) {
    text.replace(urlOne, (_,x,y) =>
                 `[${x}](${exampleUrl}${y.replace(/[^A-Za-z]+/g, '')
                                         .replace(/(?!^)[A-Z]/g, '-$&')
                                         .toLowerCase()})`) );

Regex #1 details:

  • [[[[ string
  • ((?:(?![[).)*?) – Group 1: any char other than line break chars, zero or more times, as few as possible, that does not start [[ char sequence
  • | – a | char
  • (.*?) – Group 2: any zero or more chars other than line break chars, as few as possible
  • ]] – a ]] string.

The second regex, (?!^)[A-Z], is applied to the Group 2 contents, and matches any uppercase ASCII letter that is not at the start of the string. The -$& replacement just appends - before the match.

The extra regex, .replace(/[^A-Za-z]+/g, ''), will remove all non-alpha chars from the Group 2 contents before inserting hyphens.