mdn / sprints

Archived: MDN Web Docs issues are tracked in the content repository.
https://github.com/mdn/content
Creative Commons Zero v1.0 Universal
150 stars 142 forks source link

Use of regex global flag in String.prototype.replace method example #3960

Closed chrisdavidmills closed 3 years ago

chrisdavidmills commented 3 years ago

@185driver commented on Fri Dec 11 2020

Content Issue

URL of page you are seeing the problem on:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace

Details

In line 3 of the JavaScript Demo near the top of the page, the regex code line is const regex = /dog/gi;. I would suggest that given the page relates to the replace method (and not replaceAll), the regex global flag should be removed to read const regex = /dog/i;. Doing so would provide a consistent expected output result for both examples.

I think the usefulness of this small change becomes more apparent after comparing this page with the replaceAll method here. The use of the regex global flag seems more appropriate in the latter than in the former.

Thanks for considering.

MendyBerger commented 3 years ago

I think a few more changes would make sense:

Hope you don't mind me chiming in :)

If you think all this makes sense, please assign to me

chrisdavidmills commented 3 years ago

Assigned; cheers @MendyBerger !

MendyBerger commented 3 years ago

Turns out that my point about replaceAll together with /g being redundant is wrong

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll#Non-global_regex_throws

MendyBerger commented 3 years ago

Think this is resolved now