簡體   English   中英

如何在 Reason 中將 matchAll 字符串化?

[英]How to String matchAll in Reason?

我正在嘗試使用matchAll()復制我在 javascript 中所做的事情

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

我看到 Reason 有Js.String.match但我找不到 matchAll。 我想這是因為 matchAll 是一個較新的 ecmascript。

任何關於哪個是進行高性能 matchAll 的好方法的提示? 還是我缺少特定的原因功能?

根據接受的答案,我想添加一個遵循 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;

你可以自己綁定它。 它最大的問題是它返回一個迭代器,我們也沒有綁定它。 但是我們可以使用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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM