diff --git a/SETUP/tests/jsTests/srchrepTests.js b/SETUP/tests/jsTests/srchrepTests.js index 2340d00629..c884804d51 100644 --- a/SETUP/tests/jsTests/srchrepTests.js +++ b/SETUP/tests/jsTests/srchrepTests.js @@ -99,6 +99,7 @@ QUnit.module("srchrep tests", (hooks) => { setTextValue("Example search -- and replace search text value."); $("#search").val("search"); $("#replace").val("s\\n"); + $("#is_regex").prop("checked", true); srchrep.doReplace(); assert.strictEqual( window.opener.parent.docRef.editform.text_data.value, @@ -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 value."); + $("#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 value.", + "Search replace replaces with just \\n, not newline, without regex.", + ); + assert.notOk($("#undo").prop("disabled"), "Undo should be enabled."); + }); }); diff --git a/faq/prooffacehelp.php b/faq/prooffacehelp.php index 11f48a8677..27ee84b9b1 100644 --- a/faq/prooffacehelp.php +++ b/faq/prooffacehelp.php @@ -619,11 +619,9 @@ function echo_row(string $name, string $tooltip, string $button_image_base, stri [Aa]{6} — six As of either case
A{2,8} — between 2 and 8 capital As
[hb]e — 'he' or 'be'
+ \n — new line

-

- To replace matched text with a new line, \n may be used in the replace field. -

Help

diff --git a/tools/proofers/srchrep.js b/tools/proofers/srchrep.js index c06eeaf3e5..27363dd678 100644 --- a/tools/proofers/srchrep.js +++ b/tools/proofers/srchrep.js @@ -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 \ becomes just . 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);