[英]Regular expression to replace a value with one from the same line
I need to replace the word 'foo' with the value after the comma for each line - eg 'foo'
becomes 'bar1'
.我需要用每行逗号后面的值替换单词 'foo' - 例如
'foo'
变为'bar1'
。 The output should look likes this: this.load.image('bar1', bar1);
output 应如下所示:
this.load.image('bar1', bar1);
this.load.image('foo', bar1);
this.load.image('foo', bar2);
this.load.image('foo', bar3);
...
How can this be done with a regular expression?这怎么能用正则表达式来完成?
Search:搜索:
\('[^']+',\s*([^\)]+)\);\s*$
Replacement替代品
('$1', $1);
Depending on the text editor, it could be \1
instead of $1
.根据文本编辑器,它可能是
\1
而不是$1
。 I use sublime, so I'm not sure which flavor vs code is using.我使用 sublime,所以我不确定使用的是哪种风格与代码。
Explanation:解释:
\( - Match (
'[^']+', - Match ', then all text until next ', next ' and the comma
\s* - Zero or more spaces
([^\)]+) - Capture group everything up to the next ). the group is important
\);\s*$ - Closing ); then any number of spaces and end of line.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.