search (string)
  • 18 Oct 2024
  • Dark
    Light
  • PDF

search (string)

  • Dark
    Light
  • PDF

Article summary

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 is Hello World.

  • The search pattern to match in the regexp variable is or.

    searchString-01

  • The index result will return 7 upon output, indicating the regexp ("or") is located at index position 7 in the string.

    searchString-02

Example 2

In the IAP examples shown below:

  • The reference variable for the incoming str is Hey 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.

    searchString-03

  • The index result returns 6 upon output, indicating the period is located at index position 6 in the string.

    searchString-04

Example 3

In the IAP examples shown below:

  • The incoming str variable is statically set. The reference variable is Hey Judey.

  • The regexp variable is statically set as \., a character class notation to indicate the match should be a period (".").

    searchString-05

  • The index result returns -1 upon output, indicating a period was not found in the string.

    searchString-06


Was this article helpful?

What's Next
Changing your password will log you out immediately. Use the new password to log back in.
First name must have atleast 2 characters. Numbers and special characters are not allowed.
Last name must have atleast 1 characters. Numbers and special characters are not allowed.
Enter a valid email
Enter a valid password
Your profile has been successfully updated.