diff --git a/markdown_it/common/utils.py b/markdown_it/common/utils.py index 5f1bd24b..d11d5029 100644 --- a/markdown_it/common/utils.py +++ b/markdown_it/common/utils.py @@ -152,7 +152,7 @@ def escapeHtml(raw: str) -> str: def escapeRE(string: str) -> str: - string = REGEXP_ESCAPE_RE.sub("\\$&", string) + string = REGEXP_ESCAPE_RE.sub(r"\\\g<0>", string) return string diff --git a/tests/test_port/test_misc.py b/tests/test_port/test_misc.py index b38a896b..b6866ab9 100644 --- a/tests/test_port/test_misc.py +++ b/tests/test_port/test_misc.py @@ -1,4 +1,5 @@ from importlib.util import find_spec +import re import pytest @@ -133,3 +134,10 @@ def test_backtick_closer_ending_at_pos_max(): ("code_inline", "a", "``") ] assert state.pos == 5 + + +def test_escape_re(): + """``utils.escapeRE`` backslash-escapes regex characters, as upstream does.""" + escaped = MarkdownIt().utils.escapeRE("a.b*c$") + assert escaped == r"a\.b\*c\$" + assert re.fullmatch(escaped, "a.b*c$")