From 0cc3d8e258e2cad61b9106a156ee07d560be7b3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=87=E5=AE=87=E7=A5=A5?= Date: Tue, 4 Jun 2024 20:13:37 +0800 Subject: [PATCH] Add RegExp.setLastIndex (#219) --- CHANGELOG.md | 1 + src/Core__RegExp.res | 1 + src/Core__RegExp.resi | 20 ++++++++++++++++++++ 3 files changed, 22 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8cee1d93..878eeff8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Next version +- Add `RegExp.setLastIndex` function. https://github.com/rescript-association/rescript-core/pull/219 - Add `Nullable.isNullable` function. https://github.com/rescript-association/rescript-core/pull/227 - Remove some deps to Belt, Pervasives and Js. https://github.com/rescript-association/rescript-core/pull/226/commits diff --git a/src/Core__RegExp.res b/src/Core__RegExp.res index 84668ae0..441b328a 100644 --- a/src/Core__RegExp.res +++ b/src/Core__RegExp.res @@ -15,6 +15,7 @@ module Result = { @return(nullable) @send external exec: (t, string) => option = "exec" @get external lastIndex: t => int = "lastIndex" +@set external setLastIndex: (t, int) => unit = "lastIndex" @get external ignoreCase: t => bool = "ignoreCase" @get external global: t => bool = "global" @get external multiline: t => bool = "multiline" diff --git a/src/Core__RegExp.resi b/src/Core__RegExp.resi index e16ba1e9..4e42be94 100644 --- a/src/Core__RegExp.resi +++ b/src/Core__RegExp.resi @@ -170,6 +170,26 @@ Console.log(regexp->RegExp.lastIndex) // Logs `4` to the console @get external lastIndex: t => int = "lastIndex" +/** +`setLastIndex(regexp, index)` set the index the next match will start from. + +See [`RegExp.lastIndex`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastIndex) on MDN. + +## Examples +```rescript +// Match the first word in a sentence +let regexp = RegExp.fromString("\\w+") +let someStr = "Many words here." + +regexp->RegExp.setLastIndex(4) +regexp->RegExp.exec(someStr)->ignore + +Console.log(regexp->RegExp.lastIndex) // Logs `10` to the console +``` +*/ +@set +external setLastIndex: (t, int) => unit = "lastIndex" + /** `ignoreCase(regexp)` returns whether the ignore case (`i`) flag is set on this `RegExp`.