0

I am trying to retrieve some pieces of data from a URL using regular expressions in Javascript.

const url = "/node_modules/@company/dl-atoms-control/README.md";

The following RegEx retrieves text preceded by the string "dl-" In this case "atoms":

const molecularTypePlural = url.match(/(?<=dl-)\w+/gm)[0];

I now want to use molecularTypePlural in a second RegEx to retrieve the string preceded by "dl-atoms-" which in this case is "control". I can't hard code this because the molecularTypePlural variable will have different values with different URLs. I have tried the below:

const packageNameRegex = new RegExp(`/(?<=dl-${molecularTypePlural}-)\w+/gm`);
const packageName = nodePath.match(packageNameRegex);

However the RegEx doesn't return any results. Logging packageNameRegex reveals that the RegEx seems to be incorrectly formatted:

"/\/(?<=dl-atoms-)w+\/gm/"

I think it might be a problem with escaping special characters but I'm not 100% sure. Could anyone show me where I'm going wrong. Thanks

James Howell
  • 1,392
  • 5
  • 24
  • 42

0 Answers0