Skip to content

Regex creation problem through command line interface #37

@connelly-steve

Description

@connelly-steve

Hi!
I need to replace source file paths in CSS source maps I create using a Windows batch file. I am glad I found this tool, but I had trouble getting regex search to work. Here is an example of my final answer:

call replace-in-file \"sources\":\[\".*\\\\(.*\.css)\"\] \"sources\":[\"../../src/css/$1\"] ..\..\dist\css\protections.min.css.map --isRegex
call replace-in-file \"file\":\".*\\\\(.*\.css)\" \"file\":\"../../src/css/$1\" ..\..\dist\css\protections.min.css.map --isRegex

But, to achieve this, I had to modify 'cli.js' to better split the pattern and flags:

//If the isRegex flag is passed, convert the from parameter to a RegExp object
if (isRegex) {
  //const flags = from.replace(/.*\/([gimy]*)$/, '$1');
  //const pattern = from.replace(new RegExp(`^/(.*?)/${flags}$`), '$1');
  const exp = /^(.+?)(?:\/([gimy]*))?$/.exec(from);
  const pattern = exp[1];
  const flags = exp[2] || "";
  //console.log(pattern, flags);
  try {
    options.from = new RegExp(pattern, flags);
  }
  catch (error) {
    errorHandler(error, 'Error creating RegExp from `from` parameter');
  }
}

//Log
console.log(`Replacing '${options.from}' with '${to}'`);

Before this change, I had problems with slashes and flags, whether I used flags, a trailing slash or neither of them. Furthermore, I believe logging 'options.from' makes it more transparent than logging 'from'.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions