Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.
Merged
20 changes: 16 additions & 4 deletions src/search/FindReplace.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,19 @@ define(function (require, exports, module) {
}
}

function parseDollars(replaceWith, match) {
replaceWith = replaceWith.replace(/(\$+)(\d{1,2})/g, function (whole, dollars, index) {
var parsedIndex = parseInt(index, 10);
if (dollars.length % 2 === 1 && parsedIndex !== 0) {
return dollars.substr(1) + (match[parsedIndex] || "");
} else {
return whole;
}
});
replaceWith = replaceWith.replace(/\$\$/g, "$");
return replaceWith;
}

function findNext(editor, rev) {
var cm = editor._codeMirror;
var found = true;
Expand Down Expand Up @@ -471,7 +484,7 @@ define(function (require, exports, module) {
.reverse()
.forEach(function (checkedRow) {
var match = results[$(checkedRow).data("match")],
rw = typeof replaceWhat === "string" ? replaceWith : replaceWith.replace(/\$(\d)/g, function (w, i) { return match.result[i]; });
rw = typeof replaceWhat === "string" ? replaceWith : parseDollars(replaceWith, match.result);
editor.document.replaceRange(rw, match.from, match.to, "+replaceAll");
});
_closeReplaceAllPanel();
Expand Down Expand Up @@ -560,8 +573,7 @@ define(function (require, exports, module) {
});
};
var doReplace = function (match) {
cursor.replace(typeof query === "string" ? text :
text.replace(/\$(\d)/g, function (w, i) { return match[i]; }));
cursor.replace(typeof query === "string" ? text : parseDollars(text, match));
advance();
};
advance();
Expand Down Expand Up @@ -633,4 +645,4 @@ define(function (require, exports, module) {
CommandManager.register(Strings.CMD_FIND_NEXT, Commands.EDIT_FIND_NEXT, _findNext);
CommandManager.register(Strings.CMD_REPLACE, Commands.EDIT_REPLACE, _replace);
CommandManager.register(Strings.CMD_FIND_PREVIOUS, Commands.EDIT_FIND_PREVIOUS, _findPrevious);
});
});
Loading