How to find indices of groups in JavaScript regular expressions match?

后端 未结 6 603
温柔的废话
温柔的废话 2020-12-09 09:33

When I write a regular expression like:

var m = /(s+).*?(l)[^l]*?(o+)/.exec(\"this is hello to you\");
console.log(m);

I get a match object

6条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-09 09:39

    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 = 'first
    second
    '; var regex = new Regex(pattern, 'g'); var result = regex.exec(str); console.log(5 === result.length); console.log('first'=== result[0]); console.log(''=== result[1]); console.log('first'=== result[3]); console.log(''=== 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).

提交回复
热议问题