Skip to content

Commit 3ff8e59

Browse files
committed
Add RegExp.escape binding
1 parent b63b544 commit 3ff8e59

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

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

0 commit comments

Comments
 (0)