使用Gruntreplace文件中的文本

我试图让Gruntreplace一个path引用,我不知道我做错了什么。 这看起来应该工作。 从本质上讲,我将Bootstrap文件复制到目录并更改@importpath,所以我只是试图用新的目标path'MY / NEW / DEST / PATH / bootstrap'replace'bootstrap /'。 我不想用一个模块来expression这样的东西,似乎是不必要的。 一切工作,但取代。

var destFilePath = path.join(basePath, file); // Does the file already exist? if (!grunt.file.exists(destFilePath)) { // Copy Bootstrap source @import file to destination grunt.file.copy( // Node API join to keep this cross-platform path.join(basePath, 'bootstrap/_bootstrap.scss'), destFilePath ); // Require node filesystem module, since not a global var fs = require('fs'); // Replace @import paths to be relative to new destination fs.readFile(destFilePath, 'utf8', function(err, data) { // Check for any errs during read if (err) { return grunt.log.write(err); } var result = data.replace('/bootstrap\//g', 'bootstrap/bootstrap/'); fs.writeFile(destFilePath, result, 'utf8', function(err) { return grunt.log.write(err); }); }); } 

你把你的正则expression式用引号括起来 – 不要这样做,它应该可以正常工作:

 var result = data.replace(/bootstrap\//g, 'bootstrap/bootstrap/');