Skip to content

Commit 7f61b0f

Browse files
devonestesJosé Valim
authored andcommitted
Add - to Regex.escape/1 (#5626)
We were not escaping the `-` character, commonly found in character classes, in cases where it is being used on its own. I've added a test for this new escape behavior, and also implemented the change. Signed-off-by: José Valim <[email protected]>
1 parent c830fde commit 7f61b0f

File tree

2 files changed

+2
-1
lines changed

2 files changed

+2
-1
lines changed

lib/elixir/lib/regex.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ defmodule Regex do
629629
[get_index(string, h) | get_indexes(string, t, arity - 1)]
630630
end
631631

632-
{:ok, pattern} = :re.compile(~S"[.^$*+?()\[\]{}\\\|\s#]", [:unicode])
632+
{:ok, pattern} = :re.compile(~S"[.^$*+?()\[\]{}\\\|\s#-]", [:unicode])
633633
@escape_pattern pattern
634634

635635
@doc ~S"""

lib/elixir/test/elixir/regex_test.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ defmodule RegexTest do
247247
assert matches_escaped?("# lol")
248248

249249
assert matches_escaped?("\\A.^$*+?()[{\\| \t\n\x20\\z #hello\u202F\u205F")
250+
assert Regex.match? Regex.compile!("[" <> Regex.escape("!-#") <> "]"), "-"
250251

251252
assert Regex.escape("{}") == "\\{\\}"
252253
assert Regex.escape("[]") == "\\[\\]"

0 commit comments

Comments
 (0)