【问题标题】:match regex doesn't return matched group匹配正则表达式不返回匹配组
【发布时间】:2022-01-14 07:56:59
【问题描述】:

我正在尝试解析一个 git diff 字符串以获取所有更新的 npm 包。

这是我的正则表达式 new RegExp(/^\+\s{4}\"(.*)\":\s\"(.*)\"/gm) 与此示例字符串一起使用:

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",

使用正则表达式,我应该得到所有以 + 开头的包,并且组应该有名称和版本。

Regex101

但正在运行

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

给我

[
    '+    "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"'
]

我错过了小组结果。而且我只希望名称和版本(没有空格,":),我需要两者进行进一步处理。

【问题讨论】:

标签: javascript regex


【解决方案1】:

正如 cmets 中提到的,最好的解决方案是使用 RegExp 函数 exec (mdn documenation) 来获取每场比赛的组别。你必须循环直到没有,找到结果。

这里是一个小演示(带有提供的数据和表达式)

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)

免责声明:除了演示之外,我不会在 while 循环条件中使用赋值。你可以使用表达式的lastIndex 属性,或者这样。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-26
    • 1970-01-01
    • 1970-01-01
    • 2019-01-13
    • 2021-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多