1

I'm trying to replicate what I would do in javascript with matchAll()

  const names = [
    ...withoutSlashes.matchAll(/(?<=Pos\. \d+ \- )(.*?)(?=","Importe)/g),
  ];

I see Reason has Js.String.match but I can't find the matchAll. I guess it's because matchAll is a newer ecmascript.

Any hint on which would be a good way to do a performant matchAll? or is there a specific Reason feature that I'm missing?

Matthias Winkelmann
  • 15,870
  • 7
  • 64
  • 76
cuadraman
  • 14,964
  • 7
  • 27
  • 32

2 Answers2

3

Based on the accepted answer I wanted to add a version that is following ReScript conventions. [@bs.send.pipe] is discouraged, and the ReScript language officially recommends the pipe-first operator (-> instead of |>).

like this:

[@bs.send]
external matchAll: (string, Js.Re.t) => Js.Array.array_like(array(string)) =
  "matchAll";

let matches: array(string) =
  matchAll("abc", [%re "/[a-c]/g"])->Js.Array.from;
ryyppy
  • 442
  • 3
  • 10
  • On the @ryyppy example, compiler says the type on your example should be `array(array(string)`. – armand Jun 18 '21 at 21:20
0

You can bind to it yourself. The biggest problem with it is that it returns an iterator, which we also don't have bindings for. But we can use Js.Array.array_like('a) and then convert it to an array using Js.Array.from:

[@bs.send.pipe: string]
external matchAll: Js.Re.t => Js.Array.array_like(array(string)) = "matchAll";

let matches = "abc" |> matchAll([%re "/[a-c]/g"]) |> Js.Array.from;
glennsl
  • 28,186
  • 12
  • 57
  • 75
  • 2
    We do have `Iterator` bindings in bs-fetch (and transitively in bs-webapi) now ... if adding those is not desirable then the bindings can be copied: https://github.com/reasonml-community/bs-fetch/blob/master/src/Fetch__Iterator.mli and https://github.com/reasonml-community/bs-fetch/blob/master/src/Fetch__Iterator.ml – Yawar Jul 25 '20 at 14:04