Deleting characters matching a pattern

Elm:
Deleting characters matching a pattern

How to:

Elm doesn’t natively support regex, but you can simulate character deletion. Here’s an example using String.filter to remove digits from a string.

import Browser
import Html exposing (text)

removeDigits : String -> String
removeDigits = String.filter (\char -> not (char >= '0' && char <= '9'))

main =
  text (removeDigits "Elm 0.19.1 is super 123 cool!")

-- Output: "Elm . is super  cool!"

Deep Dive

Elm lacks regex as part of its core language, differing from many other languages. This design choice aligns with Elm’s goals for simplicity and safety. Regex can be error-prone and hard to debug, but Elm advocates simpler string operations that cover many common use cases.

For cases where regex is truly needed, the implementation relies on JavaScript interop via ports. However, Elm encourages finding solutions within the language first. The String module provides functions like filter, replace, and split which cover a wide range of pattern-based text manipulation without introducing regex’s complexity.

See Also