Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor regexes with super-expressive #61

Open
carafelix opened this issue Dec 26, 2024 · 1 comment · May be fixed by #63
Open

Refactor regexes with super-expressive #61

carafelix opened this issue Dec 26, 2024 · 1 comment · May be fixed by #63

Comments

@carafelix
Copy link
Member

s.replace(new RegExp(`\\${find}`, "g"), replace);

`(\?\<\!\\S)(\?<prefix>${escapeSpecial(prefix)})\\S+(\\s|$)`,

const NOCASE_COMMAND_NAME_REGEX = /^[0-9a-z_]+$/i;

name.replace(/[0-9a-z_]/ig, "")

`${escapedPrefix}(?<command>[^@ ]+)(?:@(?<username>[^\\s]*))?(?<rest>.*)`,

make sure regex internal type is inferred (like: for regex.username)

@carafelix
Copy link
Member Author

carafelix commented Feb 12, 2025

All SuperExpressive RegEx'es must be compiled statically (specially outside functions).
Otherwise it's a performance burden.

This becomes specially relevant for

commands/src/command.ts

Lines 387 to 391 in ae66be9

const escapedPrefix = prefix.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
const commandRegex = new RegExp(
`${escapedPrefix}(?<command>[^@ ]+)(?:@(?<username>[^\\s]*))?(?<rest>.*)`,
"g",
);

and
export function getCommandsRegex(prefix: string) {
return new RegExp(
`(\?\<\!\\S)(\?<prefix>${escapeSpecial(prefix)})\\S+(\\s|$)`,
"g",
);
}

and not to obfuscate what they are doing while deconstructing them

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant