【发布时间】: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",
使用正则表达式,我应该得到所有以 + 开头的包,并且组应该有名称和版本。
但正在运行
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"'
]
我错过了小组结果。而且我只希望名称和版本(没有空格," 和 :),我需要两者进行进一步处理。
【问题讨论】:
-
String.prototype.matchAll,但也许你更喜欢
exec或类似的东西。
标签: javascript regex