(regExp: RegExp | string): (
self: string
) => Option.Option<RegExpMatchArray>Matches a string against a pattern safely and returns Option.some with the match
array, or Option.none when the pattern does not match.
Example (Matching regular expressions)
import { Option, pipe, String } from "effect"
const match = pipe("hello", String.match(/l+/))
if (Option.isSome(match)) {
console.log(`${match.value[0]}@${match.value.index}`) // "ll@2"
}
console.log(Option.isNone(pipe("hello", String.match(/x/)))) // trueexport const const match: (
regExp: RegExp | string
) => (
self: string
) => Option.Option<RegExpMatchArray>
Matches a string against a pattern safely and returns Option.some with the match
array, or Option.none when the pattern does not match.
Example (Matching regular expressions)
import { Option, pipe, String } from "effect"
const match = pipe("hello", String.match(/l+/))
if (Option.isSome(match)) {
console.log(`${match.value[0]}@${match.value.index}`) // "ll@2"
}
console.log(Option.isNone(pipe("hello", String.match(/x/)))) // true
match = (regExp: string | RegExpregExp: RegExp | string) => (self: stringself: string): import OptionOption.type Option<A> = Option.None<A> | Option.Some<A>The Option data type represents optional values. An Option<A> is either
Some<A>, containing a value of type A, or None, representing absence.
When to use
Use to represent initial values that may not yet exist
- Returning from partial functions (not defined for all inputs)
- Managing optional fields in data structures
Namespace containing utility types for Option.
When to use
Use to access type-level helpers associated with Option.
Option<RegExpMatchArray> =>
import OptionOption.const fromNullOr: <A>(
a: A
) => Option<Exclude<A, null>>
Converts a possibly null value into an Option, leaving undefined
as a valid Some.
When to use
Use when you want to treat only null as absent while preserving
undefined as a meaningful value.
Details
null → None
- Any other value (including
undefined) → Some
Example (Converting possibly null values to an Option)
import { Option } from "effect"
console.log(Option.fromNullOr(null))
// Output: { _id: 'Option', _tag: 'None' }
console.log(Option.fromNullOr(undefined))
// Output: { _id: 'Option', _tag: 'Some', value: undefined }
console.log(Option.fromNullOr(42))
// Output: { _id: 'Option', _tag: 'Some', value: 42 }
fromNullOr(self: stringself.String.match(regexp: string | RegExp): RegExpMatchArray | null (+1 overload)Matches a string with a regular expression, and returns an array containing the results of that search.
match(regExp: string | RegExpregExp))