diff --git a/CHANGELOG.md b/CHANGELOG.md index fa2de7b05f..0a6cdecaa1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,8 @@ - Fix node.js ExperimentalWarning. https://github.com/rescript-lang/rescript/pull/7379 - Fix issue with gentype and stdlib json. https://github.com/rescript-lang/rescript/pull/7378 +- Fix type of `RegExp.Result.matches`. https://github.com/rescript-lang/rescript/pull/7393 +- Add optional `flags` argument to `RegExp.fromString` and deprecate `RegExp.fromStringWithFlags`. https://github.com/rescript-lang/rescript/pull/7393 #### :house: Internal diff --git a/runtime/Stdlib_RegExp.res b/runtime/Stdlib_RegExp.res index 6d2c34f893..ff24abab95 100644 --- a/runtime/Stdlib_RegExp.res +++ b/runtime/Stdlib_RegExp.res @@ -3,12 +3,12 @@ type t module Result = { type t = array> @get_index external fullMatch: (t, @as(0) _) => string = "" - @send external matches: (t, @as(1) _) => array = "slice" + @send external matches: (t, @as(1) _) => array> = "slice" @get external index: t => int = "index" @get external input: t => string = "input" } -@new external fromString: string => t = "RegExp" +@new external fromString: (string, ~flags: string=?) => t = "RegExp" @new external fromStringWithFlags: (string, ~flags: string) => t = "RegExp" @send external test: (t, string) => bool = "test" diff --git a/runtime/Stdlib_RegExp.resi b/runtime/Stdlib_RegExp.resi index 62ce2dd19e..5415ab1d3a 100644 --- a/runtime/Stdlib_RegExp.resi +++ b/runtime/Stdlib_RegExp.resi @@ -43,7 +43,7 @@ module Result: { // This below will log "ReScript" and "is" to the console. switch regexp->RegExp.exec("ReScript is pretty cool, right?") { | None => Console.log("Nope, no match...") - | Some(result) => switch result->RegExp.Result.matches { + | Some(result) => switch result->RegExp.Result.matches->Array.keepSome { | [firstWord, secondWord] => Console.log2(firstWord, secondWord) | _ => Console.log("Didn't find exactly two words...") } @@ -51,7 +51,7 @@ module Result: { ``` */ @send - external matches: (t, @as(1) _) => array = "slice" + external matches: (t, @as(1) _) => array> = "slice" @get external index: t => int = "index" /** @@ -87,10 +87,18 @@ switch regexp->RegExp.exec("ReScript is pretty cool, right?") { | None => Console.log("Nope, no match...") | Some(result) => Console.log(result->RegExp.Result.fullMatch) // Prints "ReScript" } + +// Match 'foo' with case insensitive flag +let regexp = RegExp.fromString("foo", ~flags="i") + +switch regexp->RegExp.exec("FOO") { +| None => Console.log("Nope, no match...") +| Some(result) => Console.log(result->RegExp.Result.fullMatch) // Prints "FOO" +} ``` */ @new -external fromString: string => t = "RegExp" +external fromString: (string, ~flags: string=?) => t = "RegExp" /** `fromStringWithFlags(string)` creates a `RegExp.t` from the provided string, using the provided `flags`. This can then be used to match on strings using `RegExp.exec`. @@ -108,6 +116,7 @@ switch regexp->RegExp.exec("ReScript is pretty cool, right?") { } ``` */ +@deprecated("Use `fromString` instead") @new external fromStringWithFlags: (string, ~flags: string) => t = "RegExp" diff --git a/tests/analysis_tests/tests/src/expected/CompletionExpressions.res.txt b/tests/analysis_tests/tests/src/expected/CompletionExpressions.res.txt index 057b6130d6..cefc48617b 100644 --- a/tests/analysis_tests/tests/src/expected/CompletionExpressions.res.txt +++ b/tests/analysis_tests/tests/src/expected/CompletionExpressions.res.txt @@ -1384,7 +1384,7 @@ Path mkStuff "label": "RegExp.fromString()", "kind": 12, "tags": [], - "detail": "string => t", + "detail": "(string, ~flags: string=?) => t", "documentation": null, "insertText": "RegExp.fromString($0)", "insertTextFormat": 2