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

When *should* RegExp.exec be used? #36954

Open
DanKaplanSES opened this issue Nov 25, 2024 · 1 comment
Open

When *should* RegExp.exec be used? #36954

DanKaplanSES opened this issue Nov 25, 2024 · 1 comment
Labels
Content:JS JavaScript docs

Comments

@DanKaplanSES
Copy link
Contributor

DanKaplanSES commented Nov 25, 2024

MDN URL

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec#description

What specific section or headline is this issue about?

Description

What information was incorrect, unhelpful, or incomplete?

This list is really helpful but it only explains when you should not use exec.

- If you only care whether the regex matches a string, but not what is actually being matched, use {{jsxref("RegExp.prototype.test()")}} instead.
- If you are finding all occurrences of a global regex and you don't care about information like capturing groups, use {{jsxref("String.prototype.match()")}} instead. In addition, {{jsxref("String.prototype.matchAll()")}} helps to simplify matching multiple parts of a string (with capture groups) by allowing you to iterate over the matches.
- If you are executing a match to find its index position in the string, use the {{jsxref("String.prototype.search()")}} method instead.

Can a bullet point to be added to that explains when you should use exec? In other words, what can exec do that's unique?

What did you expect to see?

n/a

Do you have any supporting links, references, or citations?

No response

Do you have anything more you want to share?

No response

@DanKaplanSES DanKaplanSES added the needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. label Nov 25, 2024
@github-actions github-actions bot added Content:JS JavaScript docs and removed Content:JS JavaScript docs labels Nov 25, 2024
@Josh-Cena Josh-Cena added Content:JS JavaScript docs and removed needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. labels Nov 25, 2024
@Josh-Cena
Copy link
Member

Calling RegExp.exec is like calling Generator.next. You are just manually stepping through the process. There are valid use cases for it, especially if you want some fancy stepping patterns like "process one match, ignore one match", or "advance 5 characters after every match". I could add an example, but I think it's more natural to say "these are use cases you can avoid using exec for" and then you can decide whether your use case is one of them (99% of the time it should be), or not.

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

No branches or pull requests

2 participants