Skip to content

Commit 0cc3d8e

Browse files
authored
Add RegExp.setLastIndex (#219)
1 parent d68e2be commit 0cc3d8e

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Next version
44

5+
- Add `RegExp.setLastIndex` function. https://github.com/rescript-association/rescript-core/pull/219
56
- Add `Nullable.isNullable` function. https://github.com/rescript-association/rescript-core/pull/227
67
- Remove some deps to Belt, Pervasives and Js. https://github.com/rescript-association/rescript-core/pull/226/commits
78

Diff for: src/Core__RegExp.res

+1
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"

Diff for: src/Core__RegExp.resi

+20
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)