uhop / node-re2

node.js bindings for RE2: fast, safe alternative to backtracking regular expression engines.
Other
495 stars 53 forks source link

re2 capture group index start/end location of matches #152

Closed teebu closed 1 year ago

teebu commented 1 year ago

I'm trying to get index start/stop locations of group matches using RE2. I noticed it works fine in JAVA implementation of RE2. However im running into difficulties getting similar functionality with node.

I realize the d flag is specific to javascript.

var RE2 = require("re2");

// with default flags
let re = new RE2(/a (bc) (de)/d);
let result = re.exec("a bc de");
console.log(result)

result = /a (bc) (de)/d.exec("a bc de");
console.log(result)

results from re2 and built in:

[
  'a bc de',
  'bc',
  'de',
  index: 0,
  input: 'a bc de',
  groups: undefined
]

[
  'a bc de',
  'bc',
  'de',
  index: 0,
  input: 'a bc de',
  groups: undefined,
  indices: [ [ 0, 7 ], [ 2, 4 ], [ 5, 7 ], groups: undefined ]
]
uhop commented 1 year ago

“d” flag is new and not implemented (yet?).

teebu commented 1 year ago

It is fairly new for JS, but has been done in other languages. I'm not sure how this RE2 engine is made, I assume it was some kind of a port from another language.

Thanks for the quick response. Are there any plans on adding it?

uhop commented 1 year ago

There were no plans to implement it. :-) Now I think we should. I don't know when I can allocate time to do it, but PRs will be considered.

teebu commented 1 year ago

Thanks @uhop