簡體   English   中英

用同一行中的值替換值的正則表達式

[英]Regular expression to replace a value with one from the same line

我需要用每行逗號后面的值替換單詞 'foo' - 例如'foo'變為'bar1' output 應如下所示: 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.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM