Closed ibc closed 6 years ago
If I'm understanding correctly, I think what you want is something like the following (untested):
if (XRegExp.test(string, nameAddrRegExp)) {
const uris = XRegExp.match(string, sipUriRegExp, 'all');
}
Note that this would require you to remove the leading ^
and trailing $
in sipUriRegExp
for XRegExp.match
to return all matches in the string. This change should have no effect on nameAddrRegExp
because of the way XRegExp.build
already strips leading ^
s and trailing unescaped $
s in embedded regexes.
Thanks, will try that. Right now I was testing something very ugly (nideed, by also removing ^
an $
in sipUriRegExp
):
const uris = '<sip:alice@qwe.com:1234> <sips:bob@asd.net>';
XRegExp.forEach(uris, XRegExp(`<${sipUriRegExp.xregexp.source}> \\s*`, 'x'), (match, i) =>
{
console.warn(i, ': ', match);
});
But I don't think that using private API (.xregexp.source
) is the way to go.
Thanks, will try that.
Mmm, I've tested you proposal, but as the doc says, XRegExp.match
returns an array of matched strings, but what I need are an array with named backreference properties (same as exec()
returns).
ok, I think that, for my use cases in which I need to reuse lot of grammar, it's much better if I keep a map of regex strings (that contain naming groups) than keeping a map of XRegExp
objects since those cannot be later reused into most XRegExp
API calls (all but build()
).
Sounds like you can use XRegExp.forEach
along with XRegExp.build
, and push each match object (or specific backreferences) to an array within the XRegExp.forEach
callback. Not sure why <regex>.xregexp.source
would be used over XRegExp.build
here.
yes, thanks, testing that and it seems the way to go.
I've a XRegExp to parse SIP URIs as follows:
And I want to reuse the above
sipUriRegExp
within a biggernameAddrRegExp
XRegExp to parse strings that may contain multiple SIP URIs between<
and>
.It's unclear to me how to achieve this. I've tried with
XRegExp.build
:but when calling
XRegExp.exec(string, nameAddrRegExp)
it produces an array whose named groups are those in the last SIP URI in the string. I assume this is expected.May I know which is the way to go to accomplish with my need? Thanks a lot.