zoukankan      html  css  js  c++  java
  • gulp常用插件之gulp-replace使用

    更多gulp常用插件使用请访问:gulp常用插件汇总


    gulp-replace这是一款gulp3的字符串替换插件。

    更多使用文档请点击访问gulp-replace工具官网

    安装

    一键安装不多解释

    npm install --save-dev gulp-replace
    

    使用

    简单的字符串替换

    var replace = require('gulp-replace');
    
    gulp.task('templates', function(){
      gulp.src(['file.txt'])
        .pipe(replace('bar', 'foo'))
        .pipe(gulp.dest('build/'));
    });
    
    

    简单的正则表达式替换

    var replace = require('gulp-replace');
    
    gulp.task('templates', function(){
      gulp.src(['file.txt'])
        // See https://mdn.io/string.replace#Specifying_a_string_as_a_parameter
        .pipe(replace(/foo(.{3})/g, '$1foo'))
        .pipe(gulp.dest('build/'));
    });
    
    

    字符串替换为函数回调

    var replace = require('gulp-replace');
    
    gulp.task('templates', function(){
      gulp.src(['file.txt'])
        .pipe(replace('foo', function(match) {
          //替换“foo”的实例为“oof”
          return match.reverse();
        }))
        .pipe(gulp.dest('build/'));
    });
    

    正则表达式替换为函数回调

    var replace = require('gulp-replace');
    
    gulp.task('templates', function(){
      gulp.src(['file.txt'])
        .pipe(replace(/foo(.{3})/g, function(match, p1, offset, string) {
          // 用barbaz替换foobaz并记录大量信息
          // See https://mdn.io/string.replace#Specifying_a_function_as_a_parameter
          console.log('Found ' + match + ' with param ' + p1 + ' at ' + offset + ' inside of ' + string);
          return 'bar' + p1;
        }))
        .pipe(gulp.dest('build/'));
    });
    

    带文件对象的函数回调

    var replace = require('gulp-replace');
    
    gulp.task('templates', function(){
      gulp.src(['file.txt'])
        .pipe(replace('filename', function() {
           //替代对象的“文件名”的实例为“file.txt的” 
          // this.file也可用于正则表达式替换
          //参见https://github.com/gulpjs/有关可用属性的详细信息乙烯#实例的属性
          return this.file.relative;
        }))
        .pipe(gulp.dest('build/'));
    });
    
    

    API

    gulp-replace可以用字符串或正则表达式调用。

    replace(string, replacement[, options])

    • string
      类型: String
      要搜索的字符串。
    • replacement
      类型:StringFunction
      替换字符串或函数。如果replacement是函数,则每次匹配都会调用一次,并将传递要替换的字符串。
      this.file的值将等于正在处理的文件的vinyl instance实例。

    replace(regex, replacement[, options])

    • regex
      类型: RegExp
      要搜索的正则表达式模式。
    • replacement
      类型:StringFunction
      替换字符串或函数。有关特殊替换字符串模式和替换函数参数的详细信息
      this.file的值将等于正在处理的文件的vinyl instance实例。

    gulp-replaceoptions
    options是可选的第三个参数。

    • options
      类型: Object
    • options.skipBinary
      类型:boolean
      默认值:true
      跳过二进制文件。默认情况下,此选项为true。如果要替换二进制文件中的内容,则必须将其显式设置为false
  • 相关阅读:
    PSFTP使用简单教程
    JavaMail应用--通过javamail API实现在代码中发送邮件功能
    java常用数据类型转换
    自己封装的Java excel数据读取方法
    java怎样实现重载一个方法
    怎样做好测试保证交付产品质量
    软件测试之测试用例颗粒度问题
    Python 一句命令启动一个web服务器
    ansible 模块之在学习--lineinfile
    centos 7 安装sql 审核工具 inception + archer
  • 原文地址:https://www.cnblogs.com/jiaoshou/p/12184941.html
Copyright © 2011-2022 走看看