- 23 May 2023
-
DarkLight
-
PDF
search (string)
- Updated on 23 May 2023
-
DarkLight
-
PDF
search (string) Task Purpose
The search task takes a regular expression (RegExp) and a string and searches for that RegExp within the string. A RegExp can contain both special and ordinary characters. The task returns an index (number) to indicate the position where a match is found. Indexing starts from zero (0). The task returns -1
if no match is found.
Potential Use Case
The search task checks if a particular string matches for a given regex, or search pattern. You could use this method simply to check if a certain pattern exists and also know its index within a string. In conjunction with the substr
method, you could use search
to find and replace text in a data record.
Properties
Input and output properties are shown below.
Input | Type | Description |
---|---|---|
str |
String | Required. The string (or substring) to search. |
regexp |
String | Required. The search pattern to match in the regular expression. |
Output | Type | Description |
---|---|---|
index |
Number | The position where the first match between the regular expression and the given string is found. Returns -1 if not found. Of note, the outgoing index of the first character begins with 0, the second character 1, and so on. |
Example 1
In the IAP examples shown below:
-
The incoming
str
variable is statically set. The reference variable isHello World
. -
The search pattern to match in the
regexp
variable isor
. -
The
index
result will return7
upon output, indicating theregexp
("or") is located at index position 7 in the string.
Example 2
In the IAP examples shown below:
-
The reference variable for the incoming
str
isHey Dr. Livingstone
. -
The search pattern to match in the
regexp
variable is\.
, which is a special notation (character class) to indicate the match should be a period (".").Refer to this resource for more information on using character classes in regular expressions.
-
The
index
result returns6
upon output, indicating the period is located at index position 6 in the string.
Example 3
In the IAP examples shown below:
-
The incoming
str
variable is statically set. The reference variable isHey Judey
. -
The
regexp
variable is statically set as\.
, a character class notation to indicate the match should be a period ("."). -
The
index
result returns-1
upon output, indicating a period was not found in the string.