Socket
Socket
Sign inDemoInstall

globrex

Package Overview
Dependencies
0
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

globrex


Version published
Maintainers
1
Created

Package description

What is globrex?

The globrex npm package is designed to convert glob expressions into regular expressions. This allows for matching strings against complex patterns, commonly used for file matching in file systems. It supports a wide range of glob patterns, including wildcards, choice groups, and negated patterns.

What are globrex's main functionalities?

Convert glob to regex

This feature allows you to convert a simple glob pattern, like '*.js', into its equivalent regular expression. This is useful for matching filenames or strings that end with '.js'.

const globrex = require('globrex');
const { regex } = globrex('*.js');
console.log(regex); // Outputs the regex equivalent of the '*.js' glob pattern

Extended glob patterns

Globrex supports extended glob patterns, such as negated patterns using '!(pattern)'. This example demonstrates converting a negated glob pattern into a regex, which matches any '.js' file that does not start with 'pattern' or 'pattern2'.

const globrex = require('globrex');
const { regex } = globrex('!(pattern|pattern2)*.js');
console.log(regex); // Outputs the regex for negated patterns

Path mode

In path mode, with 'globstar' option enabled, globrex can convert glob patterns that match files in a directory and its subdirectories. This is particularly useful for file system operations where you need to match files across multiple directories.

const globrex = require('globrex');
const { regex } = globrex('path/**/*.js', { globstar: true });
console.log(regex); // Outputs the regex for matching any '.js' file in the 'path' directory and its subdirectories

Other packages similar to globrex

Readme

Source

Globrex

Transform glob strings into RegExp objects.

Turn a *-wildcard style glob ("*.min.js") into a JavaScript regular expression object (/^.*\.min\.js$/).

Installation

npm install globrex

Usage

const globrex = require('globrex');

const r1 = globrex('p*uck');
re.test('pot luck'); // true
re.test('pluck'); // true
re.test('puck'); // true

const r2 = globrex('*.min.js');
r2.test('http://example.com/jquery.min.js'); // true
r2.test('http://example.com/jquery.min.js.map'); // false

const r3 = globrex('*/www/*.js');
r3.test('http://example.com/ww/app.js'); // true
r3.test('http://example.com/www/lib/factory-proxy-model-observer.js'); // true

// Extended globs
const r4 = globToRegExp('*/www/{*.js,*.html}', { extended: true });
r4.test('http://example.com/www/app.js'); // true
r4.test('http://example.com/www/index.html'); // true

API

This package support all advanced features from extglob. To lean more about globbing look here:

  • mywiki.wooledge.org/glob
  • linuxjournal

globrex(glob, opts)

Type: function
Returns: RegExp

Transform a glob string into an JavaScript RegExp object. Returns a new RegExp instance.

Note: Read more about how to use RegExp.on MDN.

glob

Type: String

Glob string to transform.

opts

Type: Object
Default: { extended: false, globstar: false, strict: false, flags: '' }

Configuration object, that enables/disable different features.

opts.extended

Type: Boolean
Default: false

Whether we are matching so called "extended" globs (like bash) and should support single character matching, matching ranges of characters, group matching, etc.

Note: Interprets [a-d] as [abcd]. To match a literal -, include it as first or last character.

opts.globstar

Type: Boolean
Default: false

When globstar is false the '/foo/*' is translated a regexp like '^\/foo\/.*$' which will match any string beginning with '/foo/'.

When globstar is true, '/foo/*' is translated to regexp like '^\/foo\/[^/]*$' which will match any string beginning with '/foo/' BUT which does not have a '/' to the right of it.

E.g. with '/foo/*' these will match: '/foo/bar', '/foo/bar.txt' but these will not '/foo/bar/baz', '/foo/bar/baz.txt'

Note: When globstar is true, '/foo/**' is equivelant to '/foo/*' when globstar is false.

opts.strict

Type: Boolean
Default: false

Don't be so strict. Be forgiving about /// and make everything after the first / optional. This is how bash-globbing works.

opts.flags

Type: String
Default: ''

RegExp flags (eg "i" ) to pass in to RegExp constructor.

opts.pathsegments

Type: Boolean
Default: false

If true, returns object with both the full RegExp object AND a segments array contaiing a RegExp object for each segment in a path.

Example return value could be:

{
    segments: [ /^foo$/, /^bar$/, /^([^\/]*)$/, '^baz\\.(md|js|txt)$' ],
    full: /^foo\/bar\/.*\/baz\.\{md\,js\,txt\}$/
}

Note: This only makes sense for POSIX paths like /foo/bar/hello.js - not globbing on regular strings.

License

MIT © Terkel Gjervig

Keywords

FAQs

Last updated on 10 Apr 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc