glob-regex
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
const globRegex = require('glob-regex')
let re = globRegex('*.js')
re.test('a.js')
re.test('a.css')
re.test('a/b.js')
re = globRegex('*.jsx?')
re.test('a.js')
re.test('b.jsx')
re = globRegex('**.css')
re.test('a.css')
re.test('a/b.css')
re = globRegex('**/a.css')
re.test('a.css')
re.test('b/a.css')
re = globRegex('*.(js|css)')
re.test('a.js')
re.test('a.css')
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!