【发布时间】:2022-01-16 13:55:02
【问题描述】:
我需要用每行逗号后面的值替换单词 'foo' - 例如'foo' 变为 'bar1'。输出应如下所示:this.load.image('bar1', bar1);
this.load.image('foo', bar1);
this.load.image('foo', bar2);
this.load.image('foo', bar3);
...
如何用正则表达式做到这一点?
【问题讨论】:
我需要用每行逗号后面的值替换单词 'foo' - 例如'foo' 变为 'bar1'。输出应如下所示:this.load.image('bar1', bar1);
this.load.image('foo', bar1);
this.load.image('foo', bar2);
this.load.image('foo', bar3);
...
如何用正则表达式做到这一点?
【问题讨论】:
搜索:
\('[^']+',\s*([^\)]+)\);\s*$
替换
('$1', $1);
根据文本编辑器,它可能是\1 而不是$1。我使用 sublime,所以我不确定使用的是哪种风格与代码。
解释:
\( - 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.
【讨论】: