保留RegExp搜索值并添加

时间:2014-01-02 21:52:27

标签: regex search notepad++

如何在RegExp中返回我搜索的值,然后添加我的修饰符? 例如,以下RegExpression将搜索逗号之间的任何单词。

[^,\s][^\,]*[^,\s]*

以下是我的意见:

Biscuit, Pudding, Pizza, Beans

但是我找不到在这些单词中添加像“Cheese-”这样的单词的方法。预期的输出是:

Cheese-Biscuit, Cheese-Pudding, Cheese-Pizza, Cheese-Beans

2 个答案:

答案 0 :(得分:2)

在Notepad ++中执行CTRL+H,在替换窗口搜索模式块中,选中正则表达式。 然后在查找内容字段类型:([^,\s][^\,]*[^,\s]*)替换为字段类型:Cheese-\1然后替换所有字段,您会看到以下结果:< / p>

Cheese-Biscuit, Cheese-Pudding, Cheese-Pizza, Cheese-Beans

enter image description here

答案 1 :(得分:1)

更新 - 您已更改为记事本++。我将离开以防其他人有用。

假设Javascript(因为你提到了RegExp):

var str = "Biscuit, Pudding, Pizza, Beans";
var patt1 = /[^,\s][^\,]*[^,\s]*/g;
var result = str.replace(patt1,"Cheese-" + "$&");

输出:

  

奶酪饼干,奶酪布丁,奶酪比萨饼,奶酪豆

$&安培;插入匹配的子字符串。

/ g进行全局匹配 - 所有匹配的单词。

有关详细信息,请参阅here