1

I am looking for a nodejs script that will replace the IN with the OUT below.

IN

/* api_start ====
app.use('/api',require('./api/api'));
app.use('/sms',require('./api/sms'));
==== api_end */

or

// app.use('/api',require('./api/api'));
// app.use('/sms',require('./api/sms'));

OUT

app.use('/api',require('./api/api'));
app.use('/sms',require('./api/sms'));

Basically just uncommenting those lines of code.


or

just replace a comment like, /* api_here */ with contents from a file which would contain the middle where from the OUT above (api.js).

UPDATE

from: https://stackoverflow.com/a/25072070/3066142

The following will work if i could just write the regex to recognize that line. Below is my attempt

// require:
var replace = require("replace");

// use:
replace({
    regex: "//\s+app\.use\('/api',require\('\./api/api'\)\);", // string to be replaced
    replacement: "test worked!!", // replacement string
    paths: ['dist/server.js'], // path/to/your/file'
    recursive: true,
    silent: true,
});
Community
  • 1
  • 1
Omar
  • 2,726
  • 2
  • 32
  • 65
  • You might want to look into regexes. You can find documentation here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp – skellertor Dec 28 '18 at 16:37
  • 2
    You might try using the [strip-comments](https://www.npmjs.com/package/strip-comments) npm package. – Austin Gordon Dec 28 '18 at 16:38
  • Amazingly enough, comments are a regular expression even though basically everything else in programming languages are not regular expressions. – Patrick Roberts Dec 28 '18 at 16:40
  • I dont know regex well enough to write it myself. I think a solution to replace a comment with a file's contents like a template script would be the best solution – Omar Dec 28 '18 at 16:43
  • 1
    By the way, if you were to uncomment the first example you'd end up with `api_start ==== app.use('/api',require('./api/api')); app.use('/sms',require('./api/sms')); ==== api_end` not `app.use('/api',require('./api/api')); app.use('/sms',require('./api/sms'));` – Patrick Roberts Dec 28 '18 at 16:47
  • see my update to the original question – Omar Dec 28 '18 at 17:00

1 Answers1

1

working codepen (including more block comments, etc.): https://codepen.io/Frederic-Klein/pen/KbXxeB?editors=0012

Example code for matching and replacing the given example using regular expressions with capturing (...) and non-capturing (?:...) groups for the replacements with inline explanations:

const content = `
/* api_start ====
app.use('/api',require('./api/api'));
app.use('/sms',require('./api/sms'));
==== api_end */
// app.use('/api',require('./api/api'));
// app.use('/sms',require('./api/sms'));
`;

const regex_block_comments = /^\/\*.*\n((?:app.*\n)*)(?:.*\*\/\n)/gm;
// match lines
// starting with /*
// having the following lines start with app, capture all those lines as one group ($1 reference)
// having a line ending with */ afterwards, as non-capturing group
// repeat globally (g) and match multiline (m) string

const regex_comment = /^\/\/\s*(app.*;\n)/gm;
// match lines
// starting with //
// ignore following whitespaces
// having text "app" afterwards, capture from app to line-ending as group ($1 reference)

const replacement = `$1`;

newcontent = content.replace(regex_block_comments,replacement);
newcontent = newcontent.replace( regex_comment,replacement);

// content after replacements
console.log(newcontent);

To learn more about regular expressions and for building your patterns I recommend https://regexr.com/ and https://regex101.com/.

Frederic Klein
  • 2,846
  • 3
  • 21
  • 37