RegEx search/replace with group

If we have this code from a random Gulp file:

babelify : require('babelify'),
browserify : require('browserify'),
buffer : require('vinyl-buffer'),
changed : require('gulp-changed'),
collapse : require('bundle-collapser/plugin'),
concat : require('gulp-concat'),
cssnano : require('gulp-cssnano'),
declare : require('gulp-declare'),
gulp : require('gulp'),
gulp_handlebars : require('gulp-handlebars'),
gutil : require('gulp-util'),
htmlclean : require('gulp-htmlclean'),
jsdoc : require('gulp-jsdoc3'),
merge : require('merge-stream'),
path : require('path'),
rename : require('gulp-rename'),
sass : require('gulp-sass'),
source : require('vinyl-source-stream'),
sourcemaps : require('gulp-sourcemaps'),
transform : require('vinyl-transform'),
uglify : require('gulp-uglify'),
watchify : require('watchify'),
wrap : require('gulp-wrap'),

And quickly want to run an npm install using these modules.

We can run a search and replace with Sublime Text using this formular:

Find:

(.*) : require\('(.*)'\),
/invisible new line character here

Replace:

$2 /invisible space character here

This will find:

<any number of any character> : require('<any number of any character>'),

And replace it with the content of the second group (.*) represented by:

$2

You can now copy the whole line, and type:

npm i (and paste the clipboard content here)

into the terminal.

Voila!

 

A comma separated string to console.log();

If you have this:

, type, name, declaration, options, callback, internal, useRequired, skipEmit, uptodateName, next, packageName

Then find this:

, ([^,]+)

Replace with:

console.log( "$1", $1 );

This:

, type, name, declaration, options, callback, internal, useRequired, skipEmit, uptodateName, next, packageName

becomes this:

console.log( "type", type );
console.log( "name", name );
console.log( "declaration", declaration );
console.log( "options", options );
console.log( "callback", callback );
console.log( "internal", internal );
console.log( "useRequired", useRequired );
console.log( "skipEmit", skipEmit );
console.log( "uptodateName", uptodateName );
console.log( "next", next );
console.log( "packageName", packageName );

 

Posted in Uncategorized

Leave a Reply