Skip to content

Commit e65bd1d

Browse files
hackwalyillusionalsagacity
authored andcommitted
Add RegExp.setLastIndex (rescript-lang#219)
(cherry picked from commit 0cc3d8e)
1 parent 664812e commit e65bd1d

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- BREAKING: Fixes the type of `RegExp.Result.t` to be `array<option<string>>` instead of `array<string>`. https://github.com/rescript-association/rescript-core/pull/233.
66
- BREAKING: Adds typed bindings to `Intl`, replacing the options type of `{..}` with records. https://github.com/rescript-association/rescript-core/pull/65
77
- BREAKING: Align List api with other modules (`List.getBy` -> `List.find` etc.). https://github.com/rescript-association/rescript-core/pull/195
8+
- Add `RegExp.setLastIndex` function. https://github.com/rescript-association/rescript-core/pull/219
89
- Add `Nullable.isNullable` function. https://github.com/rescript-association/rescript-core/pull/227
910
- Remove some deps to Belt, Pervasives and Js. https://github.com/rescript-association/rescript-core/pull/226/commits
1011
- Fix: Expose Intl.Common. https://github.com/rescript-association/rescript-core/pull/197

src/Core__RegExp.res

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module Result = {
1515
@return(nullable) @send external exec: (t, string) => option<Result.t> = "exec"
1616

1717
@get external lastIndex: t => int = "lastIndex"
18+
@set external setLastIndex: (t, int) => unit = "lastIndex"
1819
@get external ignoreCase: t => bool = "ignoreCase"
1920
@get external global: t => bool = "global"
2021
@get external multiline: t => bool = "multiline"

src/Core__RegExp.resi

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,26 @@ Console.log(regexp->RegExp.lastIndex) // Logs `4` to the console
170170
@get
171171
external lastIndex: t => int = "lastIndex"
172172

173+
/**
174+
`setLastIndex(regexp, index)` set the index the next match will start from.
175+
176+
See [`RegExp.lastIndex`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastIndex) on MDN.
177+
178+
## Examples
179+
```rescript
180+
// Match the first word in a sentence
181+
let regexp = RegExp.fromString("\\w+")
182+
let someStr = "Many words here."
183+
184+
regexp->RegExp.setLastIndex(4)
185+
regexp->RegExp.exec(someStr)->ignore
186+
187+
Console.log(regexp->RegExp.lastIndex) // Logs `10` to the console
188+
```
189+
*/
190+
@set
191+
external setLastIndex: (t, int) => unit = "lastIndex"
192+
173193
/**
174194
`ignoreCase(regexp)` returns whether the ignore case (`i`) flag is set on this `RegExp`.
175195

0 commit comments

Comments
 (0)