Home > database >  regex to match specific string start and until the bracket ends
regex to match specific string start and until the bracket ends

Time:11-26

I'm looking for regex expression to take the below string and get the selected data between /* start */ and /* end */.

import {str}from'envalid';import {buildEnv}from'@leno/core';const { cleanEnv, envalidConfig } = buildEnv(process['APP_ENV'] || process.env, /* start */ {
    CLIENT_URL: str(),
    INITIAL_ACCOUNT_PAGE: str({ default: 'dashboard', devDefault: 'test', desc: 'initial page to redirect after org selection' }),
    APP_NAME: str({ default: 'CDEBase' }),
}/* end */);export{cleanEnv,envalidConfig};//# sourceMappingURL=env-config.js.map

The result of the selection should be

{
    CLIENT_URL: { default: false, devDefault: false },
    INITIAL_ACCOUNT_PAGE: { default: true, devDefault: true },
    APP_NAME: { default: true, devDefault: false },
}

CodePudding user response:

I probably don't need any regexp here. Regular indexOf might be enough.

const src = `import {str}from'envalid';import {buildEnv}from'@leno/core';const { cleanEnv, envalidConfig } = buildEnv(process['APP_ENV'] || process.env, /* start */ {
    CLIENT_URL: str(),
    INITIAL_ACCOUNT_PAGE: str({ default: 'dashboard', desc: 'initial page to redirect after org selection' }),
    APP_NAME: str({ default: 'CDEBase' }),
}/* end */);export{cleanEnv,envalidConfig};//# sourceMappingURL=env-config.js.map`;

const cleanVersion = src.substring(src.indexOf('/* start */')   11, src.indexOf('/* end */')).trim();
console.log(cleanVersion);

CodePudding user response:

I am not clear about the default requirement. Here is a solution using 3 regexes assuming this:

  • use only data within /* start */ and /* end */ => regex1
  • replace an empty str() with { default: false, devDefault: false } => regex2
  • extract content within str(...) if there is any => regex3

let input = `import {str}from'envalid';import {buildEnv}from'@leno/core';const { cleanEnv, envalidConfig } = buildEnv(process['APP_ENV'] || process.env, /* start */ {
    CLIENT_URL: str(),
    INITIAL_ACCOUNT_PAGE: str({ default: 'dashboard', devDefault: 'test', desc: 'initial page to redirect after org selection' }),
    APP_NAME: str({ default: 'CDEBase' }),
}/* end */);export{cleanEnv,envalidConfig};//# sourceMappingURL=env-config.js.map`;
const regex1 = /^.*?\/\* start \*\/ *(.*?) *\/\* end \*\/.*$/s;
const regex2 = /str\(\)/g;
const regex3 = /str\((\{.*?\})\)/g;
let result = input
  .replace(regex1, '$1')
  .replace(regex2, '{ default: false, devDefault: false }')
  .replace(regex3, '$1');
console.log(result);

Output:

{
    CLIENT_URL: { default: false, devDefault: false },
    INITIAL_ACCOUNT_PAGE: { default: 'dashboard', devDefault: 'test', desc: 'initial page to redirect after org selection' },
    APP_NAME: { default: 'CDEBase' },
}

Explanation of regex1:

  • ^ -- anchor at start of string
  • .*? -- non-greedy scan
  • \/\* start \*\/ --
  • * -- optional space
  • (.*?) -- capture group 1 with non-greedy scan, referenced in replace with $1
  • * -- optional space
  • \/\* end \*\/ --
  • .* -- greedy scan to:
  • $ -- anchor at end of string
  • use s flag so that . includes newlines
  • Related