GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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 #3

Copy link
Copy link
Closed
Closed
.exec() return only first result#3
Copy link

Description

@GeekEdem
GeekEdem
opened on Sep 23, 2016
Issue body actions

Hello. 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.

Activity

  1. teabyii commented on Sep 23, 2016

    @teabyii
    teabyii
    on Sep 23, 2016
    Contributor
    More actions

    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

  2. KarimGeiger commented on Sep 23, 2016

    @KarimGeiger
    KarimGeiger
    on Sep 23, 2016
    Contributor
    More actions

    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?

  3. teabyii commented on Sep 23, 2016

    @teabyii
    teabyii
    on Sep 23, 2016
    Contributor
    More actions

    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.

  4. KarimGeiger commented on Sep 23, 2016

    @KarimGeiger
    KarimGeiger
    on Sep 23, 2016
    Contributor
    More actions

    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?

  5. teabyii commented on Sep 23, 2016

    @teabyii
    teabyii
    on Sep 23, 2016
    Contributor
    More actions

    En ... It's a problem. I think we should have a idea for SRL "standard" to deal with differences in different languages.

  6. GeekEdem commented on Sep 23, 2016

    @GeekEdem
    GeekEdem
    on Sep 23, 2016
    Author
    More actions

    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.

  7. teabyii commented on Sep 23, 2016

    @teabyii
    teabyii
    on Sep 23, 2016
    Contributor
    More actions

    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);
    }
  8. teabyii commented on Sep 24, 2016

    @teabyii
    teabyii
    on Sep 24, 2016
    Contributor
    More actions

    @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.

    Reacted by Karim Geiger
  9. teabyii
    added 2 commits that reference this issue on Sep 25, 2016

    Update readme, release 0.2.0, close #3

    This issue will close once commit 7fff17b is merged into the 'master' branch.
    7fff17b

    Update readme, release 0.2.0, close #3

    This issue will close once commit ca38519 is merged into the 'master' branch.
    ca38519
  10. teabyii
    closed this as completedin ca38519on Sep 25, 2016
  11. teabyii commented on Sep 25, 2016

    @teabyii
    teabyii
    on Sep 25, 2016
    Contributor
    More actions

    @GeekEdem I publish 0.2.0 to give more methods to use regex conveniently, you can upgrade package and check it out: https://github.com/SimpleRegex/SRL-JavaScript#additional

    Reacted by GeekEdem
    Reacted by GeekEdem
  12. SamTebbs33 commented on Sep 25, 2016

    @SamTebbs33
    SamTebbs33
    on Sep 25, 2016
    More actions

    We have yet to decide a default in the Java implementation, perhaps we'll go with "g" as default too.

  13. GeekEdem commented on Sep 25, 2016

    @GeekEdem
    GeekEdem
    on Sep 25, 2016
    Author
    More actions

    Oh, its great! Thank you very much!

    Reacted by GeekEdem and Kent Li
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.