Bertuz
Bertuz

Reputation: 2576

how to capture the content inside nested brackets

I have the following text wrapped by a ExclusifyUnion<> type. What I want is removing with a regex that wrapper and leave what's inside its major-minor brackets.

If it helps, every type declaration will have a final >;\n. I though of a negative lookahead but with no results:

type A = ExclusifyUnion<Omit<SwitchRowContentProps, 'switch'> & {
    switch: ControlProps | undefined;
}) | (Omit<CheckboxRowContentProps, 'checkbox'> & {
    checkbox: ControlProps | undefined;
}) | OnPressRowContentProps | BasicRowContentProps | RadioRowContentProps | HrefRowContentProps | ToRowContentProps>;

type B = ExclusifyUnion<BasicBoxedRowProps | (Omit<SwitchBoxedRowProps, 'switch'> & {
    switch: ControlProps | undefined;
}) | RadioBoxedRowProps | (Omit<CheckboxBoxedRowProps, 'checkbox'> & {
    checkbox: ControlProps | undefined;
}) | HrefBoxedRowProps | ToBoxedRowProps | OnPressBoxedRowProps>;

The final result I wanna get:

type A = Omit<SwitchRowContentProps, 'switch'> & {
    switch: ControlProps | undefined;
}) | (Omit<CheckboxRowContentProps, 'checkbox'> & {
    checkbox: ControlProps | undefined;
}) | OnPressRowContentProps | BasicRowContentProps | RadioRowContentProps | HrefRowContentProps | ToRowContentProps;

type B = BasicBoxedRowProps | (Omit<SwitchBoxedRowProps, 'switch'> & {
    switch: ControlProps | undefined;
}) | RadioBoxedRowProps | (Omit<CheckboxBoxedRowProps, 'checkbox'> & {
    checkbox: ControlProps | undefined;
}) | HrefBoxedRowProps | ToBoxedRowProps | OnPressBoxedRowProps;

Upvotes: 2

Views: 37

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522762

Under the assumption that ExclusifyUnion would always occur at the top level and at most once for each type assignment, we can try the following find and replace, in regex mode:

Find:    /ExclusifyUnion<((?:(?!\b\w+(?: \w+)* =).)*)>;/gms
Replace: $1

Demo

The above regex pattern uses a tempered dot trick to ensure that each match continues up to, but not including, the proceeding type assignment line.

Upvotes: 2

Related Questions