SoluableNonagon SoluableNonagon - 3 months ago 23
Git Question

Regular expression for begins with and ends with

I'm trying to run a search with git to get me all the staged files in one of two folders: local or components. I only want to get JS files. The command runs in the console.

What I have so far:

STAGED_FILES=($(git diff --cached --name-only --diff-filter=ACM | grep "^(local|components).*?.js"))

This gets me all the staged files:

git diff --cached --name-only --diff-filter=ACM

This gets me all files paths beginning with local or components

grep "^(local|components)"

And this gets me all js files

grep ".js"

And this returns me nothing for some reason:

($(git diff --cached --name-only --diff-filter=ACM | grep "^(local|components).*?.js"))

What is the Regular Expression that I could search with that would get me all the js files in these two folders?


It didn't work because grep doesn't support ? lazy matches. You can use -E for extended regular expressions.

For example consider these

$ echo "asfasdfzasdfasdfz" | grep -E "a.*?z"
$ echo "asfasdfzasdfasdfz" | grep  "a.*?z"
$ echo "asfasdfzasdfasdf?z" | grep  "a.*?z"

As you can see without -E it tries to match ? also within the string.