Siebel eScript Language Reference > Methods Reference > Regular Expression Methods >

Get Regular Expression from String Method


The Get Regular Expression from String method searches the string that you specify in the str argument for a regular expression. It returns one of the following depending on if it finds this regular expression:

  • It finds the regular expression. It returns an array of strings that includes information about each match it finds and the property sets for these matches.
  • It does not find the regular expression. It returns the following value:

    Null

Format

regexp.exec(str)

Table 116 describes the arguments for the Get Regular Expression from String method.

Table 116. Arguments for the Get Regular Expression from String Method
Argument
Description

str

A string that this method searches for a regular expression.

Usage Without Setting the Global Attribute

Assume you configure Siebel CRM to run the Get Regular Expression from String method, you do not set the g global attribute on the regular expression instance, and the method finds a match. In this situation, the array elements that it returns include the following information:

  • Element 0. The first text in the string that matches the primary regular expression.
  • Element 1. The text that the first subpattern of the regular expression instance matches. It encloses this subpattern in parentheses.
  • Element 2 through element n. Each subsequent element uses the same format as element 1.

The returned array includes the following properties:

  • Length property. The number of text matches that exist in the returned array.
  • Index property. The start position of the first text that matches the primary regular expression.
  • Input property. The target string that the method searched.
Usage With Setting the Global Attribute

Assume you configure Siebel CRM to run the Get Regular Expression from String method but you do set the g global attribute on the regular expression instance. In this situation, this method returns the same result as if the global attribute is not set but the behavior is more complex, which allows more operations. It does the following work:

  1. Begins searching at the position in the target string that the this.lastIndex property specifies.
  2. After it finds a match, it sets the this.lastIndex property to the position after the last character in the matched text.

The this.lastIndex property possesses read and write capabilities. To find all matches of a pattern, you can configure this method to set the this.lastIndex property to the start position of the previous match that it found plus 1. This configuration causes this method to loop through a string. When it does not find a match, it resets the this.lastIndex property to 0.

Using the Get Regular Expression from String Method with the T eScript Engine

If you use code that you create with the T eScript engine, and if the Get Regular Expression from String method finds a match, then it sets the appropriate static properties of the regular expression object. For example, it sets the following properties:

  • RegExp.leftContext
  • RegExp.rightContext
  • RegExp.$n
  • And so on

This configuration provides more information about the matches.

Using the Get Regular Expression from String Method and the Get Regular Expression from StringVar Method

The behavior of the Get Regular Expression from String method and the Get Regular Expression from StringVar method varies depending on if you set the global attribute on the regular expression:

  • You do not set the global attribute. The methods return the same array. The return values and the index and input properties are the same.
  • You do set the global attribute. The methods return different arrays.

For more information, see Get Character From String Method.

Example 1

The following example calls the Get Regular Expression from String method from a regular expression whose global attribute is not set:

function fn ()
{
   var myString = new String("Better internet");
   var myRE = new RegExp(/(.).(.er)/i);
   var results = myRE.exec(myString);
   var resultmsg = "";
   for(var i =0; i < results.length; i++)
   {
      resultmsg = resultmsg + "return[" + i + "] = " + results[i] + "\n";
   }
   TheApplication().RaiseErrorText(resultmsg);
}
fn ();

This example provides the following output:

return[0] = etter   \\First text that contains primary pattern ...er (any three                     \\characters followed by "er")
return[1] = e        \\First text matching the first subpattern (.) (any single                     \\character) in the first text matching the primary pattern
return[2] = ter      \\First text matching the second subpattern (.er) (any single                      \\character followed by "er") in the first text matching                      \\the primary pattern

Example 2

The following example calls the Get Regular Expression from String method from a regular expression whose global attribute is set. This method returns all matches that exist of the primary pattern in a string of the regular expression, including matches that overlap:

function fn ()
{
   var str = "ttttot tto";
   var pat = new RegExp("t.t", "g");
   var resultmsg = "";
   while ((rtn = pat.exec(str)) != null)
   {
      resultmsg = resultmsg + "Text = " + rtn[0] + " Pos = " + rtn.index
      + " End = " + (pat.lastIndex - 1) + "\n";
      pat.lastIndex = rtn.index + 1;
   }
   TheApplication().RaiseErrorText(resultmsg)
}
fn ();

This example provides the following output:

Text = ttt Pos = 0 End = 2
Text = ttt Pos = 1 End = 3
Text = tot Pos = 3 End = 5
Text = t t Pos = 5 End = 7

Related Topics

For more information, see the following topics:

Siebel eScript Language Reference Copyright © 2018, Oracle and/or its affiliates. All rights reserved. Legal Notices.