Elm 0.19 made several improvements to the API.
regex was renamed fromString, and can no longer crashfind, replace and split were simplified, with the addition of
variations for findAtMost, replaceAtMost and splitAtMostHowMany type was removed, in favour of the separate ...AtMost
functionsSome of the old API cannot be re-implemented for Elm 0.19.
caseInsensitive used to take a Regex as input. That signature cannot be
implemented now. Instead, you would need to use fromStringWith and provide
the desired options.Create a Regex that matches patterns as specified in JavaScript.
Be careful to escape backslashes properly! For example, "\w" is escaping the
letter w which is probably not what you want. You probably want "\\w"
instead, which escapes the backslash.
In Elm 0.18, an invalid input string would crash the runtime. It is not possible to reproduce this behaviour in Elm 0.19. Thus, if given invalid input, we will instead return a
Regexthat never matches anything.
Find matches in a string:
find (AtMost 2) (regex ",") "a,b,c,d,e"
|> List.map .index
--> [1,3]
find (AtMost 2) (regex ",") "a b c d e"
|> List.map .index
--> []
find All
(regex "[oi]n a (\\w+)")
"I am on a boat in a lake."
|> List.map .match
--> ["on a boat", "in a lake"]
find All
(regex "[oi]n a (\\w+)")
"I am on a boat in a lake."
|> List.map .submatches
--> [ [Just "boat"], [Just "lake"] ]Replace matches. The function from Match to String lets
you use the details of a specific match when making replacements.
replace All
(regex "[aeiou]")
(\_ -> "")
"The quick brown fox"
--> "Th qck brwn fx"
replace (AtMost 2)
(regex "\\w+")
(\{ match } -> String.reverse match)
"deliver mined parts"
--> "reviled denim parts"
HowManyis used to specify how many matches you want to make. Soreplace Allwould replace every match, butreplace (AtMost 2)would replace at most two matches (i.e. zero, one, two, but never three or more).