GeezerVison is showing github.com — .exec() return only first result · Issue #3 · SimpleRegex/SRL-JavaScript
This repository was archived by the owner on Jun 9, 2025. It is now read-only.
This repository was archived by the owner on Jun 9, 2025. It is now read-only.
.exec() return only first result #3DescriptionHello. I have issue with returning all results to variable in JS. I build simple test, but when use
const SRL = require('srl');
const query = new SRL(query);
console.log(query.exec(query));
In console recieve:
[ 'What',
'What',
index: 0,
input: 'What is Lorem Ipsum? ... ' ]
But what about all other? And why What is 2 times?
Thanks. Activityteabyii commented on Sep 23, 2016 In JavaScript, Regex is always take the 'first-match', but not all, unless given the g flag.
In this case, you should use like this: query.all().exec('...') .
And I will make a commit to take a keyword like global in the interpreter later for SRL-JavaScript to add g flag.
About the exec, if you want to take all results matched in a regex with g flag, you should use a loop like:
const myRe = /ab*/g;
const str = 'abbcdefabh';
let myArray;
while ((myArray = myRe.exec(str)) !== null) {
var msg = 'Found ' + myArray[0] + '. ';
msg += 'Next match starts at ' + myRe.lastIndex;
console.log(msg);
}
More details: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec KarimGeiger commented on Sep 23, 2016 I'm not sure if I completely understand the problem, but wouldn't it be a good idea to apply the g flag as a default? Would that break anything? teabyii commented on Sep 23, 2016 To apply the g flag as a default will not break something, maybe a little performance loss.
I don't think it is a good idea, since it's always non-global in JavaScript. When you need in some cases, then apply g flag. KarimGeiger commented on Sep 23, 2016 Nevertheless it'd break the current SRL "standard"... How's the default behaviour in other programming languages like @SimpleRegex/java-devs or @SimpleRegex/python-devs? And, is the g flag supported there? teabyii commented on Sep 23, 2016 En ... It's a problem. I think we should have a idea for SRL "standard" to deal with differences in different languages. GeekEdem commented on Sep 23, 2016 jsenjoy, thanks for your fast responce.
The problem is not resolved with SRL implementation.
When I use:
const SRL = require('srl');
var srlQuery = "capture(uppercase letter, no whitespace, any character once or more)";
const query = new SRL(srlQuery);
console.log(query.all().exec(text));
Nothing is changed:
[ 'What',
'What',
index: 0,
input: 'What is Lorem Ipsum? ... ' ]
In second example you use not SRL, but default Regex. When I try to make this with SRL:
const SRL = require('srl');
var srlQuery = "capture(uppercase letter, no whitespace, any character once or more)";
const query = new SRL(srlQuery);
var myArray;
while ((myArray = query.exec(text)) !== null) {
console.log(myArray);
}
I receive infinity loop, what prints in console all time the same thing:
[ 'What',
'What',
index: 0,
input: 'What is Lorem Ipsum? ... ' ]
So, this 2 methods not work to get all results.
Thanks. teabyii commented on Sep 23, 2016 I am so sorry to confuse you. You should use flag g and loop, both of them, like this:
const SRL = require('srl');
var srlQuery = "capture(uppercase letter, no whitespace, any character once or more)";
const query = new SRL(srlQuery).all(); // Use all() method here.
var myArray;
while ((myArray = query.exec(text)) !== null) {
console.log(myArray);
}teabyii commented on Sep 24, 2016 @KarimGeiger I think I will make g flag as default to follow the SRL "standard" and leave more description about it in "README", unless there is a better way to solve. added 2 commits that reference this issue on Sep 25, 2016This issue will close once commit 7fff17b is merged into the 'master' branch.7fff17b This issue will close once commit ca38519 is merged into the 'master' branch.ca38519 teabyii commented on Sep 25, 2016 Reacted by GeekEdem Reacted by GeekEdem SamTebbs33 commented on Sep 25, 2016 We have yet to decide a default in the Java implementation, perhaps we'll go with "g" as default too. GeekEdem commented on Sep 25, 2016 Oh, its great! Thank you very much! Reacted by GeekEdem and Kent Li
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|