RegExp Properties

Description

RegExp type has several properties telling information about the last regular-expression operation.

These properties have two forms:a verbose property name or a shorthand name.

  • Verbose Name:input
    Short Name: $_
    The last string matched against. The input property contains the original string.
  • Verbose Name: lastMatch
    Short Name: $&
    The last matched text. The lastMatch property contains the last string that matches the entire regular expression.
  • Verbose Name:lastParen
    Short Name:$+
    The last matched capturing group. The lastParen property contains the last matched capturing group.
  • Verbose Name: leftContext
    Short Name: $`
    The text that appears in the input string prior to lastMatch. The leftContext property contains the characters of the string before the match.
  • Verbose Name: multiline
    Short Name: $*
    A Boolean value specifying whether all expressions should use multiline mode.
  • Verbose Name: rightContext
    Short Name:$'
    The text that appears in the input string after lastMatch. The rightContext property contains the characters after the match.

Example

These properties can be used to extract specific information about the operation performed by exec() or test(). Consider this example:


var text = "cat,hat,";
var pattern = /at/g;
//  w  w  w .  j a  va  2s.  co m
if (pattern.test(text)){
   console.log(RegExp.input);
   console.log(RegExp.leftContext);
   console.log(RegExp.rightContext);
   console.log(RegExp.lastMatch);
   console.log(RegExp.lastParen);
   console.log(RegExp.multiline);

   console.log(RegExp.$_);
   console.log(RegExp["$`"]);
   console.log(RegExp["$'"]);
   console.log(RegExp["$&"]);
   console.log(RegExp["$+"]);
   console.log(RegExp["$*"]);

}

The code above generates the following result.

RegExp.$1 .. RegExp.$9

RegExp.$1 through RegExp.$9 store up to nine capturing-group matches. These properties are filled in when calling either exec() or test().


var text = "cat,hat,bat,";
var pattern = /at/g;
//w ww.j  a  v a  2  s .  co m
if (pattern.test(text)){
   console.log(RegExp.$1);
   console.log(RegExp.$2);
   console.log(RegExp.$3);
}




















Home »
  Javascript »
    Javascript Introduction »




Script Element
Syntax
Data Type
Operator
Statement
Array
Primitive Wrapper Types
Function
Object-Oriented
Date
DOM
JSON
Regular Expressions