Skip to content

Add RegExp.escape binding #7695

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#### :rocket: New Feature

- Add optional `message` argument to `Result.getOrThrow` and improve default error message. https://github.com/rescript-lang/rescript/pull/7630
- Add `RegExp.escape` binding. https://github.com/rescript-lang/rescript/pull/7695

#### :nail_care: Polish

Expand Down
2 changes: 2 additions & 0 deletions runtime/Stdlib_RegExp.res
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ module Result = {
@new external fromString: (string, ~flags: string=?) => t = "RegExp"
@new external fromStringWithFlags: (string, ~flags: string) => t = "RegExp"

external escape: string => string = "RegExp.escape"

@send external test: (t, string) => bool = "test"
@return(nullable) @send external exec: (t, string) => option<Result.t> = "exec"

Expand Down
19 changes: 19 additions & 0 deletions runtime/Stdlib_RegExp.resi
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,25 @@ switch regexp->RegExp.exec("ReScript is pretty cool, right?") {
@deprecated("Use `fromString` instead") @new
external fromStringWithFlags: (string, ~flags: string) => t = "RegExp"

/**
`escape(string)` escapes any potential regex syntax characters in a string.

See [`RegExp.escape`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/escape) on MDN.

## Examples
```rescript
let literal = "foo[bar]"
let regexp = literal->RegExp.escape->RegExp.fromString
regexp->RegExp.test("foo[bar]") == true
```

## Remark

Since May 2025, this feature works across the latest devices and browser versions.
This feature might not work in older devices or browsers.
*/
external escape: string => string = "RegExp.escape"

/**
`test(regexp, string)` tests whether the provided `regexp` matches on the provided string.

Expand Down
5 changes: 5 additions & 0 deletions tests/docstring_tests/DocTest.res
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ let ignoreRuntimeTests = [
"Stdlib_Set.difference",
],
),
(
// Ignore tests that require Node.js v24+
24,
["Stdlib_RegExp.escape"],
),
]

let getOutput = buffer =>
Expand Down
4 changes: 4 additions & 0 deletions tests/docstring_tests/DocTest.res.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.