Below is the content from project.json file. I want the first occurrence of 'version' value to be replaced.
{
"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"
}
}
}
The below code using gulp-regex-replace changes all occurrences of version value to 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'));
});
How can I make this code replace only the first occurrence of version value?
The gulp-regex-replace
plugin uses global match:
https://github.com/mikrofusion/gulp-regex-replace/blob/master/index.js#L31
Another similar plugin gulp-replace accepts a custom regexp object:
replace(/__your_pattern__/, targetVersion);
Without the g
modifier in the pattern, JS will only match the first occurrence.