Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions SETUP/tests/jsTests/srchrepTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ QUnit.module("srchrep tests", (hooks) => {
setTextValue("Example search -- and replace search text <i>value</i>.");
$("#search").val("search");
$("#replace").val("s\\n");
$("#is_regex").prop("checked", true);
srchrep.doReplace();
assert.strictEqual(
window.opener.parent.docRef.editform.text_data.value,
Expand All @@ -107,4 +108,17 @@ QUnit.module("srchrep tests", (hooks) => {
);
assert.notOk($("#undo").prop("disabled"), "Undo should be enabled.");
});

QUnit.test("\\n is just backslash n without regex", function (assert) {
setTextValue("Example search -- and replace search text <i>value</i>.");
$("#search").val("search");
$("#replace").val("s\\n");
srchrep.doReplace();
assert.strictEqual(
window.opener.parent.docRef.editform.text_data.value,
"Example s\\n -- and replace s\\n text <i>value</i>.",
"Search replace replaces with just \\n, not newline, without regex.",
);
assert.notOk($("#undo").prop("disabled"), "Undo should be enabled.");
});
});
4 changes: 1 addition & 3 deletions faq/prooffacehelp.php
Original file line number Diff line number Diff line change
Expand Up @@ -619,11 +619,9 @@ function echo_row(string $name, string $tooltip, string $button_image_base, stri
[Aa]{6} &mdash; six As of either case<br>
A{2,8} &mdash; between 2 and 8 capital As<br>
[hb]e &mdash; 'he' or 'be'<br>
\n &mdash; new line<br>
</blockquote>
</p>
<p>
To replace matched text with a new line, \n may be used in the replace field.
</p>

<h4>Help</h4>
<p>
Expand Down
14 changes: 12 additions & 2 deletions tools/proofers/srchrep.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,24 @@ var srchrep = (function () {

function doReplace() {
var search = document.getElementById("search").value;
var replacetext = document.getElementById("replace").value.replace(new RegExp("\\\\n", "g"), "\r\n");
var replacetext = document.getElementById("replace").value;

saveText();
if (!document.getElementById("is_regex").checked) {
search = escapeRegExp(search);

// Replace $ in replace text with $$ to avoid special replacement patterns.
// Note, $ here is in a .replace function and must be escaped so '$$' becomes '$$$$'.
replacetext = replacetext.replace(/\$/g, "$$$$");
} else {
// Handle \n for newline. And \<x> becomes just <x>. So \\ becomes a single \.
replacetext = replacetext.replace(/\\(.)/g, (match, esc) => {
switch (esc) {
case "n":
return "\r\n";
default:
return esc; // anything else, leave as is without the backslash
}
});
}
opener.parent.docRef.editform.text_data.value = opener.parent.docRef.editform.text_data.value.replace(new RegExp(search, "gum"), replacetext);
setUndoButtonDisabled(false);
Expand Down