Javascript RegExp reference

Javascript build-in RegExp object reference.

Create regexp:

To create an object: new RegExp(exp,[flag])
You uses the new keyword to create an expession object.
To use as a function: RegExp(exp,[flag])
When the RegExp object is used as a function the return will be an expession object as well.
exp: A string that represents the search for a match. ( In this string all \ (backslash) must be types with \\ (double backslash).
flag: An optional string for global search (g), case-insensitive search (i) and multiline mode search (m). You can combine any of these characters.
An example of using the new Object(param):
<script type="text/javascript">
  var str =["We are learning javascript","I like both java and javascript",
    "Java is not the same as Javascript"];
// using new to create the object
  var pattern= new RegExp("\\bjava(\\b|\\B)","gi");
  for( var word in str) {
    // Match all Java word where the following char.= s
    document.write(str[word].replace(pattern,"[JAVA]")+ "<br>");
  }
// Used as function
  pattern= RegExp("\\bjava(\\b|\\B)","gi");
  for( var word in str) {
    // Match all Java word where the following char.= s
    document.write(str[word].replace(pattern,"[JAVA]")+ "<br>");
  }
</script>

General information about the object member types:

  1. Prototype created methods, properties or constants can only be used on instances of an object or on a primitive datatype.
  2. Constructor created methods, properties or constants can NOT be used on instances of an object or on a primitive datatype.

Prototype Methods:

Syntax: exec(string)
Performs a regular expression match of string against the regular expression. Will update the lastIndex property in a global (g) search.
Return value: Array object containing the results of the match, or null if the string did not match.
string: Used to search for an occurrence of the regular expression pattern.
An example of using the exec() :
<script type="text/javascript">
  var str =["We are learning javascript","I like both java and javascript",
    "Java is not the same as Javascript"];
  var pattern= new RegExp("\\bJava(\\b|\\B)","gi");
  for( var word in str) {
    document.write("Search string: "+str[word]+"<br>");
    pattern.lastIndex=0;
    var prevIndex=pattern.lastIndex;
    while (pattern.test(str[word])) {
      pattern.lastIndex=prevIndex;
      document.write("exec method returns: " + pattern.exec(str[word])+ "<br>");
      document.write("lastIndex for the match: " + pattern.lastIndex+ "<br>");
      prevIndex=pattern.lastIndex;
    }
    document.write("<br>");
  }
</script>

Prototype Properties:

Syntax: RegExp.prototype.constructor
This property of the RegExp.prototype holds the reference back to the RegExp object.
Important note: This is the same as the constructor property of an instantiated object

Constructor (RegExp) Properties:

Syntax: RegExp.constructor
The constructor property is a reference to the function that will be invoked to create a RegExp object.

© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.