-
Notifications
You must be signed in to change notification settings - Fork 12
Include offsets in return value #13
Comments
Can you be more specific? var str = 'abc def abc';
var regex = /a(b)c/g;
var a = regex.exec(str);
console.log(a, a.index, a.input, regex.lastIndex);
var b = regex.exec(str);
console.log(b, b.index, b.input, regex.lastIndex); logs: ["abc", "b"] (2)
0
"abc def abc"
3
["abc", "b"] (2)
8
"abc def abc"
11 |
The index of the captured group In your example you could still get the index of Example: var str = 'aabbcc';
var regex = /a+.(b)/
var a = regex.exec(str);
console.log(a, a.index, a.input, regex.lastIndex);
var b = regex.exec(str);
console.log(b, b.index, b.input, regex.lastIndex); Logs: ["aabb", "b"] 0 aabbcc 0
["aabb", "b"] 0 aabbcc 0 There's no chance to know whether the captured group Sebastian |
ah, i see what you mean. That seems like the sort of property that should just be added on the |
Sebastian |
So, the idea was to let Taking the example from your proposal: var regex = /t(e)(st(\d?))/g;
var string = 'test1test2';
var result = string.matchAll(regex);
[
{
match: "test1",
start: 0,
end: 5,
groups: [
{
match: "e",
start: 1,
end: 2,
},
{
match: "st1",
start: 2,
end: 5,
},
{
match: "1",
start: 4,
end: 5,
}
]
},
{
match: "test2",
start: 5,
end: 10,
groups: [
{
match: "e",
start: 6,
end: 7,
},
{
match: "st2",
start: 7,
end: 10,
},
{
match: "d",
start: 9,
end: 10,
}
]
}
] instead of this: [
[
"test1",
"e",
"st1",
"1"
],
[
"test2",
"e",
"st2",
"2"
]
] |
And without the "g" flag, i.e. matching against [
{
match: "test1",
start: 0,
end: 5,
groups: [
{
match: "e",
start: 1,
end: 2,
},
{
match: "st1",
start: 2,
end: 5,
},
{
match: "1",
start: 4,
end: 5,
}
]
}
] |
Why would it need a parameter tho? My thinking is that every |
You mean
Sebastian |
I definitely think it should be a separate proposal, but I'm not sure if a separate method is the best approach. |
I have to admit I never realized that. So, then it's probably ok to add a third parameter.
I still believe it would have been great to merge both, but ok. So I'll continue to write my proposal at https://github.com/SebastianZ/es-proposal-advanced-regexp-match-info. Would be great if you could review it once it's ready for being proposed to es-discuss. Sebastian |
Don't get me wrong, I think both would be great :-) but this proposal intentionally keeps it simple by returning the same thing |
Sounds reasonable. Sebastian |
A disadvantage of
String.prototype.match()
andRegExp.prototype.exec()
is also that you don't get the offsets of the captured groups.Therefore I've created https://github.com/SebastianZ/es-proposal-advanced-regexp-match-info yesterday. Though I think it might be worth to combine both to avoid another regexp function doing a similar thing.
Sebastian
The text was updated successfully, but these errors were encountered: