Skip to content

Conversation

@navya9singh
Copy link
Member

@navya9singh navya9singh commented Jul 25, 2022

Fixes #47969

@typescript-bot typescript-bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Jul 25, 2022
@typescript-bot
Copy link
Collaborator

This PR doesn't have any linked issues. Please open an issue that references this PR. From there we can discuss and prioritise.

@typescript-bot typescript-bot added For Milestone Bug PRs that fix a bug with a specific milestone and removed For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Jul 25, 2022
@amcasey
Copy link
Member

amcasey commented Jul 25, 2022

@sandersn Shouldn't this be Author: Team?

Co-authored-by: graphemecluster <[email protected]>
src/lib/es5.d.ts Outdated
* Replaces text in a string, using a regular expression or search string.
* @param searchValue A string to search for.
* @param replaceValue A string containing the text to replace for every successful match of searchValue in this string.
* @param replaceValue A string containing the text to replace. When the searchvalue is a string, only the first match is replaced. If the searchValue is a Regexp, all matches are replaced if the g flag is set. Otherwise only the first one is.
Copy link
Contributor

@Josh-Cena Josh-Cena Jul 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's an "interesting" aspect where "aaa-a".replace(/a/gy, "x") is xxx-a. I don't know how detailed we want it to be, but "all matches are replaced if the g flag is set" is technically not correct—and arguably less correct than before.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MDN's documentation for the sticky flag says that it ignores the global flag. If that's true, then I think it's fine to document it as-is, since you'll presumably find out about y long after g and hopefully read about the override.

However, is that consistent with your example? If there's some kind of interaction, it might be worth documenting.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's an open issue for it: mdn/content#1454 I just haven't gotten around to fix it.

@orta
Copy link
Contributor

orta commented Jul 27, 2022

Shouldn't this be Author: Team?

edit: https://github.com/microsoft/TypeScript/blob/main/.github/pr_owners.txt to update this (comes from here)

@amcasey
Copy link
Member

amcasey commented Jul 27, 2022

Shouldn't this be Author: Team?

edit: https://github.com/microsoft/TypeScript/blob/main/.github/pr_owners.txt to update this (comes from here)

Done. Thanks, @orta!

@sandersn
Copy link
Member

Looks pretty good but I'd like to address @Josh-Cena 's point about the sticky flag before merging.

* Replaces first match with string or all matches with RegExp.
* @param searchValue A string or RegExp search value.
* @param replaceValue A string containing the text to replace for match.
* Passes a string and {@linkcode replaceValue} to the [Symbol.replace] method on {@linkcode searchValue}. This method is expected to implement its own replacement algorithm.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Passes a string and {@linkcode replaceValue} to the [Symbol.replace] method on {@linkcode searchValue}. This method is expected to implement its own replacement algorithm.
* Passes a string and {@linkcode replaceValue} to the `[Symbol.replace]` method on {@linkcode searchValue}. This method is expected to implement its own replacement algorithm.

(Suggestion, dunno if the stdlib has its own style rules about JSDoc. Also @@replace is technically the name for the method—it's only accessed via Symbol.replace)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the formatting improvement, but I think @@replace is only useful for people who have read the spec.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'm fine with [Symbol.replace]. FWIW, on MDN we consistently use RegExp.prototype[@@replace]() so I hope it's not that obscure, but I can understand.

src/lib/es5.d.ts Outdated
* @param searchValue A string to search for.
* @param replaceValue A string containing the text to replace for every successful match of searchValue in this string.
* @param searchValue A string or regular expression to search for.
* @param replaceValue A string containing the text to replace. When the {@linkcode searchValue} is a string, only the first match is replaced. If the {@linkcode searchValue} is a RegExp, all matches are replaced if the g flag is set. Otherwise only the first one is.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I would reword this to highlight the interesting global regex case first, and try to cover strings and sticky regexes later.

Suggested change
* @param replaceValue A string containing the text to replace. When the {@linkcode searchValue} is a string, only the first match is replaced. If the {@linkcode searchValue} is a RegExp, all matches are replaced if the g flag is set. Otherwise only the first one is.
* @param replaceValue A string containing the text to replace. When the {@linkcode searchValue} is a RegExp, all matches are replaced if the g flag is set (taking into account the y flag if present). Otherwise {@linkcode searchValue} only the first match is replaced.

Copy link
Member

@sandersn sandersn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once @Josh-Cena's latest suggestion is adopted (along with my if -> is fix)

@navya9singh navya9singh merged commit b09e93d into main Sep 28, 2022
@navya9singh navya9singh deleted the fix/47969 branch September 28, 2022 15:27
@graphemecluster
Copy link
Contributor

@sandersn I can hardly understand that how come the documentation of only 2 out of 4 overloads are updated.
And replaceAll is not touched at all.
Anyway I have updated them in my PR (#50452).

@microsoft microsoft locked as resolved and limited conversation to collaborators Oct 22, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Author: Team For Milestone Bug PRs that fix a bug with a specific milestone

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

String.prototype.replace docs need fixing

9 participants