Skip to main content

regexp2.capture(comparisonString, startIndex)

Finds and tabulates sub-strings which match the target regular expression

Availability

Agent

Parameters

Name Type Description
comparisonString String A string against which the regular expression is matched
startIndex Integer An optional character index within the string at which to start matching

Returns

Array — the captured sub-strings each in tabular form

Description

This method matches the regular expression object against the string passed as its first parameter. It returns an array of tables, each of which contains values indicating the start and end of a matched sub-string found within the parameter string.

Each table contains two keys, begin and end, whose values are the indexes delimiting the sub-string within the parameter string. The array will contain as many of these tables as there are matches. If there are no matches, capture() returns null not an empty array.

Each capture group in the regular expression is enclosed within parentheses, (), and for n capture groups, the returned array will contain n + 1 results. If any particular capture group does not match, the begin and end values will both be set to -1.

Note that this is different to a zero-length string at some position within the string. For example, the regular expression "(a*)(a)" gives two matches against "a". The first, zero-length one, at index 0 is a match of zero instances of "a" at the start, which is a legitimate match.

A second, optional parameter may be passed to the method: the index within the passed string at which to begin the search for pattern matches. If this parameter is not provided, the search commences at the start of the string.

Example Code