Skip to content

split should include capture groups in results #7

@pythe

Description

@pythe

As a web developer approaching Elm from Javascript, I expect regexes and associated functions to behave the same as in JS.

Issue: In Elm 0.18 Core/Regex, an expression containing capture groups would splice captures into the returned array. In Elm 0.19 Regex, the capture groups are omitted.

Justification: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split#Description

If separator is a regular expression that contains capturing parentheses, then each time separator is matched, the results (including any undefined results) of the capturing parentheses are spliced into the output array.

Examples:

---- Elm 0.19.0 ----------------------------------------------------------------
Read <https://elm-lang.org/0.19.0/repl> to learn more: exit, help, imports, etc.
--------------------------------------------------------------------------------
> import Regex
> Regex.split (Maybe.withDefault Regex.never <| Regex.fromString ",") "a,b,c,d"
["a","b","c","d"] : List String
> Regex.split (Maybe.withDefault Regex.never <| Regex.fromString "(,)") "a,b,c,d"
["a","b","c","d"] : List String
---- elm-repl 0.18.0 -----------------------------------------------------------
 :help for help, :exit to exit, more at <https://github.com/elm-lang/elm-repl>
--------------------------------------------------------------------------------
> import Regex exposing (regex, HowMany(All))
> Regex.split All (regex ",") "a,b,c,d"
["a","b","c","d"] : List String
> Regex.split All (regex "(,)") "a,b,c,d"
["a",",","b",",","c",",","d"] : List String
// This was run in Chrome 69
console.log("a,b,c,d,e".split(/,/));
console.log("a,b,c,d,e".split(/(,)/));
VM59:1 (5) ["a", "b", "c", "d", "e"]
VM59:2 (9) ["a", ",", "b", ",", "c", ",", "d", ",", "e"]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions