The require.context function that brings files based on the regex expression.
I want to add to starts with expression to these expressions so fetch that only starts with 1-**.js
files. How can I do that?
const folders = require.context("@/views", true, /^1-.*\.js$/, "lazy")
// 1-asd.js => true
// 2-asd.js => false
// 1-bsd.js => true
The paths that the regex engine receives look like this:
The input strings that the engine regex gets contain both the folder and file name parts. Thus, to make sure you match 1-
at the start of the file name only, you need to match till the last slash:
/^(?:.*\/)?1-[^\/]*\.js$/
Or, if the /
is always present in the paths, you can shorten the regex to
/^.*\/1-[^\/]*\.js$/
\/1-[^\/]*\.js$
Details:
^
- start of string(?:.*\/)?
- an optional sequence of any zero or more chars other than line break chars as many as possible and then /
1-
- 1-
string[^\/]*
- zero or more chars other than /
\.js$
- .js
at the end of string.See the regex demo