Get index of each capture in a JavaScript regex

后端 未结 7 710
一个人的身影
一个人的身影 2020-11-29 04:43

I want to match a regex like /(a).(b)(c.)d/ with \"aabccde\", and get the following information back:

\"a\" at index = 0
\"b\" at i         


        
相关标签:
7条回答
  • 2020-11-29 05:22

    Based on the ecma regular expression syntax I've written a parser respective an extension of the RegExp class which solves besides this problem (full indexed exec method) as well other limitations of the JavaScript RegExp implementation for example: Group based search & replace. You can test and download the implementation here (is as well available as NPM module).

    The implementation works as follows (small example):

    //Retrieve content and position of: opening-, closing tags and body content for: non-nested html-tags.
    var pattern = '(<([^ >]+)[^>]*>)([^<]*)(<\\/\\2>)';
    var str = '<html><code class="html plain">first</code><div class="content">second</div></html>';
    var regex = new Regex(pattern, 'g');
    var result = regex.exec(str);
    
    console.log(5 === result.length);
    console.log('<code class="html plain">first</code>'=== result[0]);
    console.log('<code class="html plain">'=== result[1]);
    console.log('first'=== result[3]);
    console.log('</code>'=== result[4]);
    console.log(5=== result.index.length);
    console.log(6=== result.index[0]);
    console.log(6=== result.index[1]);
    console.log(31=== result.index[3]);
    console.log(36=== result.index[4]);
    

    I tried as well the implementation from @velop but the implementation seems buggy for example it does not handle backreferences correctly e.g. "/a(?: )bc(def(\1ghi)xyz)/g" - when adding paranthesis in front then the backreference \1 needs to be incremented accordingly (which is not the case in his implementation).

    0 讨论(0)
提交回复
热议问题