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.

Inconsistent RegExp test results

See original GitHub issue

Hi Svaarala,

I have this code to validate a guid enclosed with braces:

var reGuid = /^\{[0-9A-F]{8}\-[0-9A-F]{4}\-[0-9A-F]{4}\-[0-9A-F]{4}\-[0-9A-F]{12}\}$/g;

var m1 = reGuid.test("{A6BFBB23-4426-4CFB-8D2F-5A642566BF94}");
var m2 = reGuid.test("{A6BFBB23-4426-4CFB-8D2F-5A642566BF94}");
var m3 = reGuid.test("{A6BFBB23-4426-4CFB-8D2F-5A642566BF94}");
var m4 = reGuid.test("{A6BFBB23-4426-4CFB-8D2F-5A642566BF94}");

The expected result is true for all four variables but the output is:

m1=true
m2=false
m3=true
m4=false

Seems the regex state after a test is wrong and cannot restart but a test that fails, resets the internal state correctly.

Regards, Mauro.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
svaaralacommented, Jul 2, 2017

@fatcerberus Related, the .lastIndex property of the RegExp instance tracks the offset where to resume from. It has pretty weird semantics if you resume matching a different string though; you can also set it manually if you don’t want to resume matching a string to completion.

1reaction
fatcerberuscommented, Jul 2, 2017

I didn’t realize that’s what the global flag was for. I thought it just meant to match all occurrences (rather than stopping after the first), didn’t know it was also “resumable”.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why am I seeing inconsistent JavaScript logic behavior ...
With Firebug running, a watch expression containing your call to test() will result in intermittent false results showing up, either in your ...
Read more >
RegEx.test() returns alternating results | by Nikhil John | Medium
If the regex has the global flag set, test() will advance the lastIndex of the regex. A subsequent use of test() will start...
Read more >
Inconsistent results from Regular Expression (Regex)
I'm having difficulty understanding the regex functionality. I have created a simple list with default values to try to isolate the issue ...
Read more >
Inconsistent RegEx Results - Does Field Type Make a Difference
I am trying to parse dozens of entries. Specifically, I am trying to pull a contract number from a body of text. But,...
Read more >
Why do I get inconsistent results when using the same REGEX ...
Why do I get inconsistent results when using the same REGEX pattern in different REGEX functions? I am quite new to REGEX syntax,...
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