I'm trying to create a sentence where each word, space, and punctuation character is in it's own HTML span. It would be for a language learning app so each span can be linked to the dictionary entry for that word. So I want something like String.split but with a regex as the delimiter, and to keep the delimiters as list items.
Basically exactly the same as this guy: Split a string into an array of words, punctuation and spaces in JavaScript, but in Elm.
I want to go from:
"Hello there. These are two sentences." : String
and get to:
["Hello", " ", "there", ".", " ", "These", " ", "are", " ", "two", " ", "sentences", "."] : List String
I'm pretty bamboozled so far.
Regards Chris
CodePudding user response:
Presumably you can just translate the solution directly to elm?
So using elm/regex
something like:
import Regex exposing (Regex)
wordSplitRegex : Regex
wordSplitRegex =
Regex.fromString "\\w |\\s |[^\\s\\w] "
|> Maybe.withDefault Regex.never
split : String -> List String
split input =
Regex.find wordSplitRegex input
|> List.map .match