https://github.com/aleclarson/glob-regex
Tiny glob-to-RegExp converter
https://github.com/aleclarson/glob-regex
glob isomorphic regexp tiny
Last synced: 9 months ago
JSON representation
Tiny glob-to-RegExp converter
- Host: GitHub
- URL: https://github.com/aleclarson/glob-regex
- Owner: aleclarson
- License: mit
- Created: 2017-11-26T00:12:49.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-12-15T09:43:22.000Z (over 7 years ago)
- Last Synced: 2024-12-28T13:46:10.723Z (over 1 year ago)
- Topics: glob, isomorphic, regexp, tiny
- Language: JavaScript
- Homepage:
- Size: 15.6 KB
- Stars: 8
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# glob-regex
[](https://www.npmjs.com/package/glob-regex)
[](https://bundlephobia.com/result?p=glob-regex)
[](https://packagephobia.now.sh/result?p=glob-regex)
[](https://paypal.me/alecdotbiz)
Convert a glob to a `RegExp` object.
- Any periods are escaped (`.` -> `\\.`)
- `*` and `**` are replaced
- Always start with `^` and end with `$`
- All `RegExp` syntax is valid
- Path separators are auto-escaped by `new RegExp`
```js
const globRegex = require('glob-regex')
// Match no directory.
let re = globRegex('*.js')
re.test('a.js') // => true
re.test('a.css') // => false
re.test('a/b.js') // => false
// Use ? operator for optional character.
re = globRegex('*.jsx?')
re.test('a.js') // => true
re.test('b.jsx') // => true
// Match any directory.
re = globRegex('**.css')
re.test('a.css') // => true
re.test('a/b.css') // => true
// Match any directory and specific name.
re = globRegex('**/a.css')
re.test('a.css') // => true
re.test('b/a.css') // => true
// Use | operator to match multiple values.
re = globRegex('*.(js|css)')
re.test('a.js') // => true
re.test('a.css') // => true
```
Use `globRegex.replace()` to transform a glob into a RegExp-compatible string.
**NOTE:** It's not recommended to use `globRegex(array)` if you need
the `exec` method, since the result will be difficult to make use of.
Using the `test` method works great, though!