global flag in regular expression fails test
See original GitHub issueLink to the challenge: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/regular-expressions/check-for-mixed-grouping-of-characters
Issue
Adding a global flag to the regular expression has the test fail for the first two assertions.
let myString = "Eleanor Roosevelt";
let myRegex = /(Franklin|Eleanor).*Roosevelt/g;
let result = myRegex.test(myString);
This can be confusing considering also how the challenge is introduced, since the code snippet explaining the concept shows the global flag being used.
let testStr = "Pumpkin";
let testRegex = /P(engu|umpk)in/g;
testRegex.test(testStr);
Suggestion
Update the code snippet introducing the challenge:
-let testRegex = /P(engu|umpk)in/g;
+let testRegex = /P(engu|umpk)in/;
However, since the global flag should not affect the outcome of the test (to what I can ascertain), a better suggestion would be to update the test itself, to account for its presence.
Issue Analytics
- State:
- Created 4 years ago
- Comments:9 (9 by maintainers)
Top Results From Across the Web
Why does a RegExp with global flag give wrong results?
A RegExp object with the g flag keeps track of the lastIndex where a match occurred, so on subsequent matches it will start...
Read more >Why is my regex working intermittently? - DEV Community
When the RegExp test method is run with global flag (/g), the regex keeps internally the state of the search. Therefore at each...
Read more >The flag /g of JavaScript's regular expressions - 2ality
With the flag /g set, test() returns true as many times as there are matches in the string. lastIndex contains the index after...
Read more >RegExp.prototype.global - JavaScript - MDN Web Docs
The g flag indicates that the regular expression should be tested against all possible matches in a string. Each call to exec() will...
Read more >JavaScript Regex Global Flag (Example) - Coderwall
Do not use the global flag when testing. I'll use this as my example: var example = 'The tale of mats and bats';...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Agreed.
This challenge focuses on teaching the user about groups - the use of
test
overmatch
seem incidental to me, as does the presence of the flag.Also, the challenge asks you to make sure
so it’s only asking you to use
test
againstmyString
once.It might be better to remove the global flag from the challenge example unless we also explain how
test()
works with the global flag and give some info on lastIndex.I think in order to really know if we should fail the test when using the global flag we would have to consider how the code is supposed to run. Are we “emulating” that each
test()
is being run in isolation or are they executed one after the other.