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: update trivial regex capture group find and replace challenge #37768
refactor: update trivial regex capture group find and replace challenge #37768
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@PartyLich In addition to the change I have suggested, there should be an additional test which validates at least three capture groups were used. Otherwise, a simple replace would solve the challenge without even using regex.
...-and-data-structures/regular-expressions/use-capture-groups-to-search-and-replace.english.md
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See my suggested changes.
...-and-data-structures/regular-expressions/use-capture-groups-to-search-and-replace.english.md
Outdated
Show resolved
Hide resolved
...-and-data-structures/regular-expressions/use-capture-groups-to-search-and-replace.english.md
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@PartyLich Let's change the huhText
which means nothing to str
.
Also, you need to figure out a way to not allow a solution like the following which does not use replacement patterns:
let str = "one two three";
let fixRegex = /(\w+) (\w+) (\w+)/; // Change this line
let replaceText = "three two one"; // Change this line
let result = str.replace(fixRegex, replaceText);
...-and-data-structures/regular-expressions/use-capture-groups-to-search-and-replace.english.md
Outdated
Show resolved
Hide resolved
...-and-data-structures/regular-expressions/use-capture-groups-to-search-and-replace.english.md
Outdated
Show resolved
Hide resolved
...-and-data-structures/regular-expressions/use-capture-groups-to-search-and-replace.english.md
Outdated
Show resolved
Hide resolved
We could use a regex to ensure there is some instance of - text: <code>replaceText</code> should use parenthesized submatch string(s).
testString: assert(replaceText.match(/(\$\d{1,2})+(?:[\D]|\b)/g)); Do we care that they use >1 submatch in the final replacement? It would make sense to me to use all 3 of the mandated capture groups in the replacement as well, but idk if we want to enforce that. |
new test added. let str = "one two three";
let fixRegex = /(\w+) (\w+) (\w+)/; // Change this line
let replaceText = "$3 $2 $1"; // Change this line
let result = str.replace(fixRegex, replaceText); let replaceText = "three $2 $1"; // Change this line let replaceText = "three two $1"; // Change this line failing let str = "one two three";
let fixRegex = /(\w+) (\w+) (\w+)/; // Change this line
let replaceText = "three two one"; // Change this line
let result = str.replace(fixRegex, replaceText); "Parenthesized submatch string" is interesting wording...but it matches MDN should a student happen to use that source for research/help. I'm obviously open to suggested replacements. |
I do not like the fact that the following pass: let replaceText = "three $2 $1"; // Change this line let replaceText = "three two $1"; // Change this line The user should only be using spaces and $n for the replacement and not hard-coded values. |
That's why my previous comment included the question "Do we care that they use >1 submatch in the final replacement?". Whether they use 1 or 3 or n, they're still demonstrating that they are familiar with the concept and can utilize it to update a string. |
Let's make sure they use all 3. |
OK, I think this one looks good now. |
...-and-data-structures/regular-expressions/use-capture-groups-to-search-and-replace.english.md
Outdated
Show resolved
Hide resolved
testString: assert(code.match(/result\s*=\s*str\.replace\(.*?\)/)); | ||
- text: <code>fixRegex</code> should use at least three capture groups. | ||
testString: assert((new RegExp(fixRegex.source + '|')).exec('').length - 1 >= 3); | ||
- text: <code>replaceText</code> should use parenthesized submatch string(s). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
replaceText
should use parenthesized submatch string(s).
If I'm new to regex, going solely by the challenge description, I have no idea what "parenthesized submatch string(s)" means. Probably should be rephrased.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any suggestions on what to rephrase it to? I didn't come across or previously know an alternate term. If I were a new(er) student and making use of the "Read-Search-Ask" slogan that was so prominent on FCC, a google search of the phrase would produce accurate results (for multiple programming languages). If I were me and had the habit of looking up js docs on MDN, the String.prototype.replace() page has this exact wording.
I have no strong feelings about changing it to something else, I just have no idea what would be better. I could use some help there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An/Or I suppose we could add the preferred terminology to the description. At present it says something to the effect of "use dollar signs", but offers no terminology to concisely describe that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since you have now edited the instructions to add a little detail about the $
syntax, perhaps this could be (re)used to simplify the terminology -
text:
replaceText
should use parenthesized submatch string(s) (ie. the nth parenthesized submatch string,$n
, corresponds to the nth capture group).
The MDN docs on .replace()
uses this terminology.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In addition to the suggested changes, the challenge instruction should also include what to do with the fixRegex
and replaceText
variables in the editor. Currently, the instruction just states -
Write a regex using three capture groups that will replace the string "one two three" with the string "three two one" and assign the result to the replaceText variable.
You shouldn't be required to look at the tests to figure out what the variables are for -
fixRegex should use at least three capture groups.
replaceText should use parenthesized submatch string(s).
We could use something on the lines of what we had in the previous version -
Write a regex so that it will search for the string "good". Then update the replaceText variable to replace "good" with "okey-dokey"
Previous challenge could be completed without any capture groups. New challenge requires 3 capture groups and 3 uses of the submatches in the replacement. resolves #17645
Use solution from SO (https://stackoverflow.com/questions/16046620/regex-to-count-the-number-of-capturing-groups-in-a-regex) as suggested by @RandellDawson to count the number of capturing groups, regardless of whether or not the regex in question solves the broader problem. Co-Authored-By: Randell Dawson <5313213+RandellDawson@users.noreply.github.com>
Co-Authored-By: Randell Dawson <5313213+RandellDawson@users.noreply.github.com>
...-and-data-structures/regular-expressions/use-capture-groups-to-search-and-replace.english.md
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor grammar fix.
testString: assert(code.match(/result\s*=\s*str\.replace\(.*?\)/)); | ||
- text: <code>fixRegex</code> should use at least three capture groups. | ||
testString: assert((new RegExp(fixRegex.source + '|')).exec('').length - 1 >= 3); | ||
- text: <code>replaceText</code> should use parenthesized submatch string(s). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since you have now edited the instructions to add a little detail about the $
syntax, perhaps this could be (re)used to simplify the terminology -
text:
replaceText
should use parenthesized submatch string(s) (ie. the nth parenthesized submatch string,$n
, corresponds to the nth capture group).
The MDN docs on .replace()
uses this terminology.
Co-Authored-By: Manish Giri <manish.giri.me@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thank You for your contribution.
…ge (freeCodeCamp#37768) * refactor: replace trivial regex capture group challenge Previous challenge could be completed without any capture groups. New challenge requires 3 capture groups and 3 uses of the submatches in the replacement. resolves freeCodeCamp#17645 * test: validate at least 3 capture groups were used Use solution from SO (https://stackoverflow.com/questions/16046620/regex-to-count-the-number-of-capturing-groups-in-a-regex) as suggested by @RandellDawson to count the number of capturing groups, regardless of whether or not the regex in question solves the broader problem. Co-Authored-By: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> * refactor: more meaningful variable name Co-Authored-By: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> * test: add parenthesized substring test * test: ensure 3 uses of substring replacement * refactor: reword instructions * fix: minor grammar fix Co-Authored-By: Manish Giri <manish.giri.me@gmail.com> * refactor: elaborate wording in test
Implements the fixes provided by @alanpaulprice in pull #36925 with
the changes requested by maintainers. Previous challenge could be
completed without any capture groups. New challenge rectifies this
behavior.
resolves #17645
Update index.md
)master
branch of freeCodeCamp.Note: as the commit text says, this is the pull referenced above with the requested corrections. Not my work, basically.
Closes #17645