Giter Club home page Giter Club logo

multiregexp2's Introduction

MultiRegExp2

This class gets all matches, with start and end position, within the string, for a given regexp.

From high level the source code is:

  1. Ensuring that each character is captured by a group: eg. /ab(cd)ef/ => /(ab)(cd)(ef)/
  2. Calling exec on the converted regexp with a given string
  3. Summing lengths of previous groups for start position of current group, add length of current group for end position

API

MultiRegexp2(baseRegExp: Regexp)

will setup parsed regexp, returns instance

execForAllGroups(string: string, includeFullMatch: boolean)

will find all matching groups, returns array<{match: string, start: Number, end: Number}>

execForGroup(string: string, group: Number)

will find match for group number, returns {match: string, start: Number, end: Number}

Usage

let regex = /a(?: )bc(def(ghi)xyz)/g;
let regex2 = new MultiRegExp2(regex);

let matches = regex2.execForAllGroups('ababa bcdefghixyzXXXX');
console.log(matches);
// reset to beginning: regex2.regexp.lastIndex = 0;

Will output:

[ { match: 'defghixyz', start: 8, end: 17 },
  { match: 'ghi', start: 11, end: 14 } ]

If you want to include the full match (group 0) then add true as the second parameter.

Also available:

let matches = regex2.execForGroup('ababa bcdefghixyzXXXX', 2);
= { match: 'ghi', start: 11, end: 14 }

Contribution

to compile the module to ES5 run npm install && npm run build && npm run test

multiregexp2's People

Contributors

dependabot[bot] avatar qjebbs avatar valoricde avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

multiregexp2's Issues

Wrong start position

Hi
There is a problem when i use following regex: ((http)(s))(:\/\/)

The problem is in group4. When we run above regex on something like https://regex101.com, the start index of group 4 should be 5, but 10 given!

Sorry for bad English

Another wrong case

Here's a case that doesn't currently parse correctly:
/\[(?:a=(.*?))?(?:b=(.*?))?]/

The regex it generates internally is
/(\[)(?:(a=)(.*?)()?)(?:(b=)(.*?))?]/

Notice that the first optional capture group from the original has been converted into a non-optional capture group!
ie

                    | There should be a ? here before the )
(\[)(?:(a=)(.*?)()?)(?:(b=)(.*?))?]
                |-| This optional capture group shouldn't exist?
(\[)(?:(a=)(.*?)()?)(?:(b=)(.*?))?]

Update: I tried to simplify the regex when i opened the issue but I realised that the simplification wasn't valid because I was using non-greedy capture groups without an end character in the regex

another parsing problem

code:

let r = new MultiRegExp2(/a(?:(b))?/);
console.log(r.regExp.source);

result:

(a(?:)(b))?

expected:

(a)(?:(b))?

wrong capture index

Please look into this code:

let str = "aaaaaaaaa";
let reg = /((a{2})a(\2))/ig;
let mreg = new MultiRegExp2(reg);
let matches = mreg.execForAllGroups(str, true);
for (let m of matches) {
    console.log(m.match, m.start, m.end);
}

output:

aaaaa 0 5
aa 0 2
aa 3 5

expected:

aaaaa 0 4
aa 0 1
aa 3 4

some case wrong

like next line RegExp, which with repeat symbol ( + or * ), it will be wrong
/(ba)+.(a*)/

regexp.execForAllGroups('bababaaaaa', true);
matches:  [ { match: 'ba', start: 0, end: 2 },
  { match: 'aaa', start: 3, end: 6 } ]

I think the regexp should change to /((ba)+).(a*)/ when calculate the next match position

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.