1

I'm struggling a bit with a RegExp result. Basically what I want to have, is a startIndex which gives me the exact position for each match related to the source string. So instead of an array with strings

var regex = /(?:\w*) from "(\w*)"/g
var result = regex.exec('import foo from "foo"');
console.log(result.slice(1)) // => ["foo"]

I would like to have an object something like this:

[{
  value: 'foo',
  startIndex: 17
}]

I don't think this is possible without using a special library, so my question is:

Do you know any library which returns a more precise match information or any other solution how I can solve it?

Note this is just an example RegExp. In the real application I will have more than 10 completely different RegExp. Therefore, a dynamic approach is preferred.

Addition

I need this for the next major version of the GitHub Linker. Currently the link replacement is done with a jquery selector, which isn't that flexible and sometimes wrong. A RegExp will hopefully decrease the overhead so other developers can easily extend the GitHub Linker with other languages like Ruby, Python .... Currently just JavaScript is supported.

That's the reason why I'm looking for a solid and flexible solution.

– Thanks

stefanbuck
  • 429
  • 1
  • 3
  • 10
  • 1
    @PaulRoub: I don't think it's a duplicate and what you links answers the question. – Casimir et Hippolyte Nov 10 '15 at 01:46
  • @PaulRoub The solution provided in the link would work if the string would not contain two times 'foo'. So it's not that easy to find the correct position. – stefanbuck Nov 10 '15 at 01:49
  • 1
    @stefanbuck: I participated in that question as a commenter, and I'm sure that it's the best you can get out of JavaScript's limited API. – nhahtdh Nov 10 '15 at 03:05
  • @stefanbuck in you case ,contain two times `foo` ,just use `lastIndexOf` instead `indexOf` function – Kerwin Nov 10 '15 at 03:53
  • What do you need the index for? Replacement could be done with replacement callback anyway. – nhahtdh Nov 10 '15 at 03:55

1 Answers1

-1

The accepted answer here seems to be close. I've adapted it somewhat below, and it appears to get near to what was required.

var str = 'import foo from "foo" import bar from "bar"'
var regex = /(?:\w*) from "(\w*)"/g;
var output = [];
while ((result = regex.exec(str))) {
  var  offset = result[0].lastIndexOf(result[1]);
  output.push('Value: ' + result[1] + ', startIndex: ' + (result.index+offset));
}

document.getElementById('results').innerHTML = output;
<div id="results"></div>
Community
  • 1
  • 1
sideroxylon
  • 4,338
  • 1
  • 22
  • 40
  • The OP wants the start index of the capturing group 1, not the start index of the whole match (which is returned by exec). – nhahtdh Nov 10 '15 at 03:53
  • Mhhh ... this could work if the RegExp doesn't contain the value self at the end e.g.: ```var str = 'import foo from "foo" foo' var regex = /(?:\w*) from "(\w*)" foo/g;``` I'm not sure if this is a big problem in my use case ... I have to check it. – stefanbuck Nov 10 '15 at 08:24
  • @stefanbuck you can use positive lookahead `(?:\w*) from "(\w*)" (?=\1)` – Kerwin Nov 10 '15 at 09:36