Home > OS >  How to write regEx for node modules path?
How to write regEx for node modules path?

Time:11-10

I have the following library inside my node_modules folder C:\myProject\node_modules\@ng-select\ng-option-highlight

I need to write regex which will point to this file path ( webpack needs regex )

So what i did until now is this

/node_modules\/@ng-select/,

this is code that i found from somewhere else I don't know even if it is correct now if i try to add ng-option-highlight I get error from webpack

An unhandled exception occurred: Invalid regular expression flags

/node_modules/@ng-select/ng-option-highlight

How can i edit this regex file path here ?

This is my webpack config file

const webpack = require('webpack');
const TerserPlugin = require("terser-webpack-plugin");
const linkerPlugin = require("@angular/compiler-cli/linker/babel");
const path = require('path');
module.exports = {
    /**
     * Remove all unused MomentJS locales
     * The problem is described in this article
     * https://medium.jonasbandi.net/angular-cli-and-moment-js-a-recipe-for-disaster-and-how-to-fix-it-163a79180173
     */
    plugins: [
        new webpack.ContextReplacementPlugin(/moment[/\\]locale$/, /de|en|fr|it/),
    ],
    module: {
        rules: [
            {
                include: /node_modules/,
                test: /\.mjs$/,
                type: 'javascript/auto'
            }
        ]
    },
    plugins: [
        new webpack.DefinePlugin({
            ngDevMode: true,
        }),
        new webpack.LoaderOptionsPlugin({
            options: {
                rules: [
                    {
                      test: /.*\.d.ts$/,
                      include: /node_modules\/@ng-select/,
                      use: {
                        loader: 'babel-loader',
                        options: {
                          configFile: false,
                          plugins: [linkerPlugin],
                        }
                      }
                    }
                ]
            }
        })
    ],
}

CodePudding user response:

To indicate that something is a regex, a regex is enclosed with slashes, like so: /<regex>/. In this specific case, that's rather confusing, because paths often start and end with slashes as well. To write a path in a regex, we need to enclose it with slashes and escape the inner slashes using backslashes to not confuse the parser. The value for your new path will thus be /node_modules\/@ng-select\/ng-option-highlight/.

  • Related