question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

global flag in regular expression fails test

See original GitHub issue

Link 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:closed
  • Created 4 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
ojeytonwilliamscommented, Dec 9, 2019

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.

Agreed.

Are we “emulating” that each test() is being run in isolation or are they executed one after the other.

This challenge focuses on teaching the user about groups - the use of test over match seem incidental to me, as does the presence of the flag.

Also, the challenge asks you to make sure

the regex that you have created is checked against myString and either true or false is returned depending on whether the regex matches.

so it’s only asking you to use test against myString once.

1reaction
lasjorgcommented, Dec 9, 2019

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.

const names = ['Franklin D. Roosevelt', 'Eleanor Roosevelt', 'Franklin Rosevelt'];
const myRegex = /(Franklin|Eleanor).*Roosevelt/g;

for (const name of names) {
  console.log(myRegex.test(name));
  // true, false, false
}
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found