用正则表达式替换掉

时间:2019-01-06 13:48:28

标签: angularjs gulp gulp-uglify gulp-replace

我正在使用gulp replace通过匹配开始和结束字符串来用另一个名称替换完整路径名

  

示例

输入:

Firefox (version 63.0.3)
var myWindow = window.open("", "newWindow", "toolbar=yes, width=600, height=600", "resizable,scrollbars");

myWindow.document.write("<input class='printFriendly' type='button' " + "onClick='window.print(); return false;'" + "value='Print This Page'/>");

输出

function abc() {

   var myWindow = window.open("", "newWindow", "toolbar=yes, width=600, height=600", "resizable,scrollbars");

    myWindow.document.write("<input class='printFriendly' type='button' " + "onClick='window.print(); return false;'" + "value='Print This Page'/>");

    myWindow.document.write("<input class='printFriendly' type='button' " + "onClick='CheckAnswer(" + "\"" + myAns + "\"" + ")'" + "value='check Answers'/>"); 

    // creates a <table> element
    var tbl1=document.createElement("table");
    var row = document.createElement("tr");
    tbl1.appendChild(row);

    var noAnsDiv = document.createElement("Div");
    noAnsDiv.setAttribute("id","printNoAnsDiv");
    noAnsDiv.append(tbl1);
    // display table in the window
    myWindow.document.body.append(noAnsDiv);
}


src/app/Client/Home/home.service.js

在这里我想用上述输出在所有状态下替换.js文件路径


dist/Home/home.service.min.js

但是它不起作用,没有得到匹配的路径 因此,如果有人有解决的想法。 预先感谢。

2 个答案:

答案 0 :(得分:0)

尝试:

gulp.task('templates', () => {

  return gulp.src(['dist/client/app/js/app.config.min.js'])

      // need the matching groups 2 and 3 as well, and the m flag

      //.pipe(replace(/^(src\/app\/Client\/)(.*)(.js)$/gm, 'dist/$2$3'))

      .pipe(replace(/(src\/app\/Client\/)(.*)(.js)/g, 'dist/$2$3'))

      // escape the .
      .pipe(replace(/\.js/g, '.min.js'))

      .pipe(gulp.dest('dist/client/app/js/'));
});

src/app/Client/Home/home.service.js

==>

dist/Home/home.service.min.js

[而且您也需要我在其中添加的return语句。]

答案 1 :(得分:0)

对于每个this answergulp-replace将把回调作为第二个参数。唯一的窍门是完全匹配将是第一个参数,所有匹配组将是第二个到第n个参数,然后还有另外两个。

E.G。

replace("my-(.*)", (full, capture) => {return "string: " + capture})