Skip to main content

regexp.capture(compareString, startIndex)

Finds the first match against the target regular expression and tabulates group captures within the match

Availability

Device + Agent

Parameters

Name Type Description
compareString 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 matched sub-string and each group capture in tabular form

Description

This method compares the target regular expression against the characters comprising the passed string and returns an array of tables which indicate the first location of a sub-string whose characters match the regular expression and then any capture group matches within that sub-string.

Important Because of shortcomings in Squirrel’s regexp, we strongly recommend that you implement regular expressions in your agent code using regexp2.capture() as a drop-in replacement for regexp.capture().

If there is no match, capture() returns null not an empty array. If there is a match, but there no capture group matches within the sub-string (or no groups in the regular expression), only the matched sub-string is included in the array.

Each table contains just two keys, begin and end, whose values are the indexes delimiting the sub-string within the main string.

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. You can use this to iterate through the comparison string to find multiple matches against the target regular expression.

Example Code