Inconsistent RegExp test results
See original GitHub issueHi 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:
- Created 6 years ago
- Comments:6 (4 by maintainers)
Top 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 >
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 Free
Top 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

@fatcerberus Related, the
.lastIndexproperty 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.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”.