A zero-dependency ES module to scan file contents and pull structured line details with capture variables using regular expressions.
Add the pattern collector to your Node.js project using npm or yarn:
npm install pattern-collector-anyjs-pull-lines
Or using yarn:
yarn add pattern-collector-anyjs-pull-lines
Import the default function and pass your code text along with your regex configurations:
import pullLines from 'pattern-collector-anyjs-pull-lines';
const results = pullLines({
fileContent: sourceCode,
importRegex: {
parseRegex: /import\s*\{[^}]*router\s+as\s+(\w+)[^}]*\}\s*from\s*['"]\.\/([^/]+)\/.*['"]/,
searchString: /^[ \t]*import\b.*from\s+['"]\.[^'"]*['"];/gm
},
consumptionRegex: {
parseRegex: /router\.use\s*\(\s*['"`]\/?([^'"`]+)['"`]\s*,\s*(\w+)/,
searchString: /^[ \t]*router\.use\b.*?;/gm
}
});
The library default exports a function taking a configuration object:
pullLines({ fileContent, importRegex, consumptionRegex, showLog, showLogStep1 })
| Parameter | Type | Description |
|---|---|---|
| fileContent | string | The raw text / source code of the file to search. |
| importRegex | Object |
Contains matching settings for imports:
• searchRegex / searchString (RegExp): Scans for the lines.
• parseRegex (RegExp): Extracts capturing variables (e.g. Variable, Folder).
|
| consumptionRegex | Object |
Contains matching settings for route consumptions:
• searchRegex / searchString (RegExp): Scans for usage lines.
• parseRegex (RegExp): Extracts capturing variables (e.g. Endpoint, Variable).
|
| showLog | boolean | Optional. Enables verbose node-level console logging. |
Input your source code and regular expressions below to test line extraction in real-time in the browser.