diff --git a/common/space_lua/stdlib/string.ts b/common/space_lua/stdlib/string.ts index c67cb6d8..003d40cb 100644 --- a/common/space_lua/stdlib/string.ts +++ b/common/space_lua/stdlib/string.ts @@ -39,6 +39,7 @@ function translatePattern(pattern: string): string { // Replace single backslash with double backslashes pattern = pattern.replace(new RegExp("\\\\", "g"), "\\\\"); + pattern = pattern.replace(new RegExp("\\|", "g"), "\\|"); for (const [key, value] of Object.entries(ROSETTA_STONE)) { pattern = pattern.replace(new RegExp(key, "g"), value); @@ -138,7 +139,7 @@ export const stringApi = new LuaTable({ repl: any, // string or LuaFunction n = Infinity, ) => { - pattern = translatePattern("" + pattern); + pattern = translatePattern(pattern); const replIsFunction = repl.call; let count = 0, diff --git a/common/space_lua/stdlib/string_test.lua b/common/space_lua/stdlib/string_test.lua index b932d609..1e8de795 100644 --- a/common/space_lua/stdlib/string_test.lua +++ b/common/space_lua/stdlib/string_test.lua @@ -32,6 +32,15 @@ local result, count = string.gsub("hello world", "hello", "hi") assert(result == "hi world", "Basic string replacement failed") assert(count == 1, "Basic replacement count failed") +-- https://github.com/silverbulletmd/silverbullet/issues/1326 +result, count = ("abc|de"):gsub("|", "-") +assert(result == "abc-de", "replacements failed [got: " .. result .. ", expected: abc-de]") +assert(count == 1, "replacement count failed") + +result, count = ("abc\\|de|"):gsub("|", "-") +assert(result == "abc\\-de-", "replacements failed [got: " .. result .. ", expected: abc\\-de-]") +assert(count == 2, "replacement count failed") + -- Multiple replacements result, count = string.gsub("hello hello hello", "hello", "hi") assert(result == "hi hi hi", "Multiple replacements failed")