1

我正在尝试复制我在 javascript 中所做的事情matchAll()

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

我看到原因有Js.String.match,但我找不到 matchAll。我想这是因为 matchAll 是一个较新的 ecmascript。

任何关于哪个是进行高性能 matchAll 的好方法的提示?还是我缺少特定的原因功能?

4

2 回答 2

3

根据接受的答案,我想添加一个遵循 ReScript 约定的版本。[@bs.send.pipe]不鼓励,ReScript 语言官方推荐使用管道优先操作符(->而不是|>)。

像这样:

[@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;
于 2020-08-25T08:28:26.427 回答
0

你可以自己绑定它。它最大的问题是它返回一个迭代器,我们也没有绑定它。但我们可以使用Js.Array.array_like('a),然后使用以下方法将其转换为数组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;
于 2020-07-25T12:19:51.700 回答