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

5
  • 1
    @PaulRoub: I don't think it's a duplicate and what you links answers the question. Commented Nov 10, 2015 at 1: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. Commented Nov 10, 2015 at 1: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. Commented Nov 10, 2015 at 3:05
  • @stefanbuck in you case ,contain two times foo ,just use lastIndexOf instead indexOf function Commented Nov 10, 2015 at 3:53
  • What do you need the index for? Replacement could be done with replacement callback anyway. Commented Nov 10, 2015 at 3:55

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

3 Comments

The OP wants the start index of the capturing group 1, not the start index of the whole match (which is returned by exec).
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 you can use positive lookahead (?:\w*) from "(\w*)" (?=\1)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.