如何gulp-regex-只替换第一场比赛?

时间:2017-07-07 07:56:55

标签: javascript gulp gulp-replace

以下是project.json文件中的内容。我希望第一次出现'版本'值得替换。

{
  "title": "Project Title",
  "copyright": "Copyright Info",
  "description": "Project Description",
  "version": "1.0.0",
  "buildOptions": {
    "xmlDoc": true
  },
  "dependencies": {
    "Microsoft.NETCore.App": {
      "version": "1.0.1",
      "type": "platform"
    }
  }
}

使用gulp-regex-replace的以下代码将所有出现的版本值更改为1.0.1。

var targetVersion= "1.0.1";
gulp.task('replace', function () {
    return gulp.src(['./src/**/project.json'])
        .pipe(bom())
        .pipe(replace({ regex: '\\n\\s\\s\\"version\\"\\:\\s\\"([0-9a-zA-Z.-]+)\\"', replace: `${targetVersion}` }))
        .pipe(gulp.dest('./src'));
});

如何才能使此代码仅替换第一次出现的版本值?

1 个答案:

答案 0 :(得分:1)

CREATE TRIGGER [MY_TABLE_Inserts] ON MY_TABLE AFTER INSERT AS BEGIN insert into MY_TABLE (TYPE, NAME, VALUE, DESCRIPTION) select 2, NAME, VALUE, 'INSERT OPERATION' from inserted where TYPE = 1; END CREATE TRIGGER [MY_TABLE_Inserts] ON MY_TABLE AFTER UPDATE AS BEGIN insert into MY_TABLE (TYPE, NAME, VALUE, DESCRIPTION) select 2, NAME, VALUE, 'UPDATE OPERATION' from inserted where TYPE = 1; END CREATE TRIGGER [MY_TABLE_Inserts] ON MY_TABLE AFTER DELETE AS BEGIN insert into MY_TABLE (TYPE, NAME, VALUE, DESCRIPTION) select 2, NAME, VALUE, 'DELETE OPERATION' from deleted where TYPE = 1; END 插件使用全局匹配: https://github.com/mikrofusion/gulp-regex-replace/blob/master/index.js#L31

另一个类似的插件gulp-replace接受自定义正则表达式对象:

gulp-regex-replace

如果模式中没有replace(/__your_pattern__/, targetVersion); 修饰符,JS将仅匹配第一次出现。