Skip to content

Commit 2646aea

Browse files
authored
Add RegExp.escape binding (#7695)
* Add RegExp.escape binding * Skip RegExp.escape docstring tests on node versions below v24 * Add CHANGELOG entry
1 parent 812d07e commit 2646aea

File tree

5 files changed

+31
-0
lines changed

5 files changed

+31
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#### :rocket: New Feature
2020

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

2324
#### :nail_care: Polish
2425

runtime/Stdlib_RegExp.res

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ module Result = {
1212
@new external fromString: (string, ~flags: string=?) => t = "RegExp"
1313
@new external fromStringWithFlags: (string, ~flags: string) => t = "RegExp"
1414

15+
external escape: string => string = "RegExp.escape"
16+
1517
@send external test: (t, string) => bool = "test"
1618
@return(nullable) @send external exec: (t, string) => option<Result.t> = "exec"
1719

runtime/Stdlib_RegExp.resi

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,25 @@ switch regexp->RegExp.exec("ReScript is pretty cool, right?") {
121121
@deprecated("Use `fromString` instead") @new
122122
external fromStringWithFlags: (string, ~flags: string) => t = "RegExp"
123123

124+
/**
125+
`escape(string)` escapes any potential regex syntax characters in a string.
126+
127+
See [`RegExp.escape`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/escape) on MDN.
128+
129+
## Examples
130+
```rescript
131+
let literal = "foo[bar]"
132+
let regexp = literal->RegExp.escape->RegExp.fromString
133+
regexp->RegExp.test("foo[bar]") == true
134+
```
135+
136+
## Remark
137+
138+
Since May 2025, this feature works across the latest devices and browser versions.
139+
This feature might not work in older devices or browsers.
140+
*/
141+
external escape: string => string = "RegExp.escape"
142+
124143
/**
125144
`test(regexp, string)` tests whether the provided `regexp` matches on the provided string.
126145

tests/docstring_tests/DocTest.res

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ let ignoreRuntimeTests = [
3030
"Stdlib_Set.difference",
3131
],
3232
),
33+
(
34+
// Ignore tests that require Node.js v24+
35+
24,
36+
["Stdlib_RegExp.escape"],
37+
),
3338
]
3439

3540
let getOutput = buffer =>

tests/docstring_tests/DocTest.res.js

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)