Skip to content

Commit

Permalink
Add RegExp.setLastIndex (#219)
Browse files Browse the repository at this point in the history
  • Loading branch information
hackwaly authored Jun 4, 2024
1 parent d68e2be commit 0cc3d8e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions src/Core__RegExp.res
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module Result = {
@return(nullable) @send external exec: (t, string) => option<Result.t> = "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"
Expand Down
20 changes: 20 additions & 0 deletions src/Core__RegExp.resi
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down

0 comments on commit 0cc3d8e

Please sign in to comment.