如何在gulp-replace中使用捕获组? [英] How to use a Capture Group with gulp-replace?
问题描述
口香糖替换0.5.4
gulp-replace 0.5.4
当前:
<link rel="stylesheet" href="assets/styles/app-3c224ff086.css">
目标:
<link rel="stylesheet" href="assets/styles/app-3c224ff086.css" media="screen">
结果:
<link rel="stylesheet" href="$1" media="screen">
初始测试为: https://regex101.com/r/miM7TN/1 .我可能已将测试链接到$ 0捕获组,但是在我的项目中,我使用的是捕获组1.
Initial test was: https://regex101.com/r/miM7TN/1 . I might have linked the test with a capture group of $0, but in my project I am using capture group 1.
在我的gulp任务中添加了以下内容:
In my gulp task the following is added:
var regex = /\"assets\/styles\/app-+.*\"/;
var subst = `$1 media="screen"`;
.pipe(replace(regex, subst))
找到我的字符串,包括开始和结束引号.而不是捕获组,我得到的是原汁原味的$ 1.如何获取捕获组的实际值? gulp-replace文档显示替换值'$ 1foo'. SO上类似的问题显示"$ 1"和"$ 1",我还没有看到与我的情况有所不同的地方.我尝试过反引号,单引号和双引号.
My string, to include start and end quotes, is being found. Instead of a capture group I am getting a literal $1. How do I get the actual value of the capture group? The gulp-replace documentation shows a replace value '$1foo'. Similar questions on SO show '$1' and "$1", and I am not yet seeing the difference to my situation. I've tried back-ticks, single quotes, and double quotes.
推荐答案
$1
后向引用引用ID为1的捕获组,即使用模式中的第一个(...)
捕获的文本.
The $1
backreference refers to a capturing group with ID 1, the text captured with the first (...)
in your pattern.
您必须使用
var regex = /("assets\/styles\/app-+.*")/;
使$1
能够像$&
一样工作"(对整个匹配值的反向引用).
for $1
to "work" the same way as $&
(backreference to the whole match value).
这篇关于如何在gulp-replace中使用捕获组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!