diff options
-rw-r--r-- | jim-regexp.c | 5 | ||||
-rw-r--r-- | tests/regexp.test | 5 |
2 files changed, 9 insertions, 1 deletions
diff --git a/jim-regexp.c b/jim-regexp.c index 25ffd21..25e05a3 100644 --- a/jim-regexp.c +++ b/jim-regexp.c @@ -496,7 +496,10 @@ int Jim_RegsubCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv) continue; } else { - Jim_AppendString(interp, resultObj, replace_str + j - 1, 2); + /* If the replacement is a trailing backslash, just replace with a backslash, otherwise + * with the literal backslash and the following character + */ + Jim_AppendString(interp, resultObj, replace_str + j - 1, (j == replace_len) ? 1 : 2); continue; } } diff --git a/tests/regexp.test b/tests/regexp.test index 7fa3ee0..a4e3f73 100644 --- a/tests/regexp.test +++ b/tests/regexp.test @@ -644,6 +644,11 @@ test regexp-21.14 {Literal newline in pattern} { regexp -all -inline "\n(\[ \t\]+)" "\n\t\t# This is a test" } "{\n\t\t} {\t\t}" +test regexp-21.15 {Replace literal backslash} { + regsub -all <bs> {<bs>abc<bs>def} \\ value + set value +} "\\abc\\def" + test regexp-22.1 {effect of caching} jim { set filedata {BEGIN_TABLE ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; END_TABLE} |