Skip to content
This repository has been archived by the owner on Sep 6, 2021. It is now read-only.

Commit

Permalink
Merge pull request #5840 from SAPlayer/replace-double-dollar
Browse files Browse the repository at this point in the history
Fixing issue with double dollars in replace string
  • Loading branch information
Narciso Jaramillo committed Nov 8, 2013
2 parents 0dd45e7 + 3d3a3d8 commit 08eb10b
Show file tree
Hide file tree
Showing 2 changed files with 389 additions and 4 deletions.
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 @@ -472,7 +485,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 @@ -561,8 +574,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 @@ -634,4 +646,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

0 comments on commit 08eb10b

Please sign in to comment.