|Home | Tutorial | Classes | Functions | QSA Workbench | Language | Qt API | QSA Articles | Qt Script for Applications |

[Prev: Size] [Home] [Next: String]

#### RegExp

A `RegExp` is a regular expression matcher for strings.

Regular expressions can be created either by using the `/expression/` syntax or by using the RegExp constructor as shown below. Note that when using the RegExp constructor, a string is passed, and all backslashes must be escaped using an extra backslash, i.e. ``\d. Below are two ways to create regular expressions that matches the pattern "QUANTITY=471 # Order quantity" and gets the number 471.

        var directRegex = /([A-Z]+)=(\d+)/;
        var str = "QUANTITY=471 # Order quantity";
        str.match(directRegex);
        directRegex.capturedTexts[2]; // "471";

        var indirectRegex = new RegExp( "([A-Z]+)=(\\d+)" );

##### RegExp Properties

- valid : Boolean; Returns true if the regular expression is syntactically valid; otherwise returns false.

- empty : Boolean; Returns true if the pattern is empty; otherwise returns false.

- matchedLength : Number; The length of the last matched string, or -1 if there was no match.

- capturedTexts : String[]; An array of all the captured texts from the previous match. This can be empty.

- global : Boolean; Specifies that the regexp should be matched globally. A global regexp will match every occurrence (i.e. as many times as possible), whereas a non-global regexp will match at most once (at the first match it encounters). This is particularly relevant for replace where every occurance of a pattern will be replaced when global is true.

  A regular expression can be set to global either by setting the global property on a regexp object or by specifying a trailing `g` in the pattern.

      var re = /mypattern/g;     // Global by method #1

      var re = /mypattern/;
      re.global = true           // Global by method #2

- ignoreCase : Boolean; Specifies that the regexp ignores case when matching. Case-insensitivity can is enabled by either specifying a trailing `i` after the pattern or by setting the `ignoreCase` property.

      var re = /mypattern/i;     // Case-insensitive by method #1

      var re = /mypattern/;
      re.ignoreCase = true;      // Case-insensitive by method #2

##### RegExp Functions

- toString() : String; Returns the regular expression pattern as a string.

- search( text : String ) : Number;

          var re = /\d+ cm/; // matches one or more digits followed by space then 'cm'
          re.search( "A meter is 100 cm long" ); // returns 11

  Searches `text` for the pattern defined by the regular expression. The function returns the position in the text of the first match or -1 if no match is found.

- searchRev( text : String ) : Number; Same as `search()`, but searchRev searches from the end of the text.

- exactMatch( text : String ) : Boolean; Returns true if `text` exactly matches the pattern in this regular expresssion; otherwise returns false.

- cap( nth : Number ) : String;

          re = /name: ([a-zA-Z ]+)/;
          re.search( "name: John Doe, age: 42" );
          re.cap(0);  // returns "name: John Doe"
          re.cap(1);  // returns "John Doe"
          re.cap(2);  // returns undefined, no more captures.

  Returns the `nth` capture of the pattern in the previously matched text. The first captured string (`cap(0)` ) is the part of the string that matches the pattern itself, if there is a match. The following captured strings are the parts of the pattern enclosed by parenthesis. In the example above we try to capture `([a-zA-Z ]+)`, which captures a sequence of one or more letters and spaces after the `name:` part of the string.

- pos( nth : Number ) : Number;

          re = /name: ([a-zA-Z ]+)/;
          re.search( "name: John Doe, age: 42" );
          re.pos(0); // returns 0, position of "name: John Doe"
          re.pos(1); // returns 6, position of "John Doe"
          re.pos(2); // returns -1, no more captures

  Returns the position of the `nth` captured text in the search string.

[Prev: Size] [Home] [Next: String]