match regex doesn't return matched group

Viewed 56

I'm trying to parse a git diff string to get all updated npm packages.

This is my regex new RegExp(/^\+\s{4}\"(.*)\":\s\"(.*)\"/gm) using with this example string:

index ab40f85..dc6d42f 100755
--- a/package.json
+++ b/package.json
@@ -1,7 +1,6 @@
{
"name": "name",
"version": "0.0.0",
"repository": {
    "type": "git",
@@ -79,7 +78,7 @@
    "next": "12.0.4",
    "next-auth": "^3.29.0",
    "next-with-apollo": "^5.2.1",
-    "passport": "^0.5.0",
+    "passport": "^0.4.0",
    "passport-jwt": "^4.0.0",
    "passport-local": "^1.0.0",
    "pdfkit": "^0.13.0",
@@ -113,18 +112,18 @@
    "@graphql-codegen/typescript-react-apollo": "^3.2.1",
    "@nestjs/schematics": "^8.0.0",
    "@nestjs/testing": "^8.0.0",
-    "@nrwl/cli": "13.2.3",
-    "@nrwl/cypress": "13.2.3",
-    "@nrwl/eslint-plugin-nx": "13.2.3",
-    "@nrwl/jest": "13.2.3",
-    "@nrwl/linter": "13.2.3",
-    "@nrwl/nest": "13.2.3",
-    "@nrwl/next": "13.2.3",
-    "@nrwl/node": "13.2.3",
-    "@nrwl/react": "13.2.3",
-    "@nrwl/tao": "13.2.3",
-    "@nrwl/web": "13.2.3",
-    "@nrwl/workspace": "13.2.3",
+    "@nrwl/cli": "13.2.4",
+    "@nrwl/cypress": "13.2.4",
+    "@nrwl/eslint-plugin-nx": "13.2.4",
+    "@nrwl/jest": "13.2.4",
+    "@nrwl/linter": "13.2.4",
+    "@nrwl/nest": "13.2.4",
+    "@nrwl/next": "13.2.4",
+    "@nrwl/node": "13.2.4",
+    "@nrwl/react": "13.2.4",
+    "@nrwl/tao": "13.2.4",
+    "@nrwl/web": "13.2.4",
+    "@nrwl/workspace": "13.2.4",
    "@testing-library/cypress": "^8.0.1",
    "@testing-library/dom": "^8.11.1",
    "@testing-library/jest-dom": "^5.15.0",

With the regex I should get all packages starting with an + and the groups should have the name and the version.

Regex101

But running

const updated = diff.match(regex)
console.log(updated)

gives me

[
    '+    "passport": "^0.4.0"',
    '+    "@nrwl/cli": "13.2.4"',
    '+    "@nrwl/cypress": "13.2.4"',
    '+    "@nrwl/eslint-plugin-nx": "13.2.4"',
    '+    "@nrwl/jest": "13.2.4"',
    '+    "@nrwl/linter": "13.2.4"',
    '+    "@nrwl/nest": "13.2.4"',
    '+    "@nrwl/next": "13.2.4"',
    '+    "@nrwl/node": "13.2.4"',
    '+    "@nrwl/react": "13.2.4"',
    '+    "@nrwl/tao": "13.2.4"',
    '+    "@nrwl/web": "13.2.4"',
    '+    "@nrwl/workspace": "13.2.4"'
]

I'm missing the group results. And I would expect only the names and the versions (without spaces, " and :) and I need both for further processing.

1 Answers

As it was mentioned in the comments, the best solution would be to use the RegExp function exec (mdn documenation) to get the groups, for each match. you would have to loop until no, result is found.

Here a small demo (with the provided data and expression):

let expression = new RegExp(/^\+\s{4}\"(.*)\":\s\"(.*)\"/gm)
let data = `index ab40f85..dc6d42f 100755
--- a/package.json
+++ b/package.json
@@ -1,7 +1,6 @@
{
"name": "name",
"version": "0.0.0",
"repository": {
    "type": "git",
@@ -79,7 +78,7 @@
    "next": "12.0.4",
    "next-auth": "^3.29.0",
    "next-with-apollo": "^5.2.1",
-    "passport": "^0.5.0",
+    "passport": "^0.4.0",
    "passport-jwt": "^4.0.0",
    "passport-local": "^1.0.0",
    "pdfkit": "^0.13.0",
@@ -113,18 +112,18 @@
    "@graphql-codegen/typescript-react-apollo": "^3.2.1",
    "@nestjs/schematics": "^8.0.0",
    "@nestjs/testing": "^8.0.0",
-    "@nrwl/cli": "13.2.3",
-    "@nrwl/cypress": "13.2.3",
-    "@nrwl/eslint-plugin-nx": "13.2.3",
-    "@nrwl/jest": "13.2.3",
-    "@nrwl/linter": "13.2.3",
-    "@nrwl/nest": "13.2.3",
-    "@nrwl/next": "13.2.3",
-    "@nrwl/node": "13.2.3",
-    "@nrwl/react": "13.2.3",
-    "@nrwl/tao": "13.2.3",
-    "@nrwl/web": "13.2.3",
-    "@nrwl/workspace": "13.2.3",
+    "@nrwl/cli": "13.2.4",
+    "@nrwl/cypress": "13.2.4",
+    "@nrwl/eslint-plugin-nx": "13.2.4",
+    "@nrwl/jest": "13.2.4",
+    "@nrwl/linter": "13.2.4",
+    "@nrwl/nest": "13.2.4",
+    "@nrwl/next": "13.2.4",
+    "@nrwl/node": "13.2.4",
+    "@nrwl/react": "13.2.4",
+    "@nrwl/tao": "13.2.4",
+    "@nrwl/web": "13.2.4",
+    "@nrwl/workspace": "13.2.4",
    "@testing-library/cypress": "^8.0.1",
    "@testing-library/dom": "^8.11.1",
    "@testing-library/jest-dom": "^5.15.0",`
   
   let match;
   let result = []
   while (match = expression.exec(data)){
    result.push(match);
   }
   
   console.info(result)

Disclaimer: apart from a demo, I would not use the assignment in the while loop condition. you could use the lastIndex property of the expression, or so.

Related