*TAXES

*TECHNOLOGY

SERVICES

CRYSTAL COMPUTER CONCEPTS

DELAWARE'S   ·      MIDDLETOWN  ·     ODESSA  ·    TOWNSEND AREA

Regular Expressions

Background

All computer languages have a vocabulary to allow the programmers to write code. When the code’s vocabulary follows a specific syntax, the computer has instructions to follow. When the code is interpreted by the computer, the instructions are executed. Accordingly, the program “runs.” However, the program may not run or properly execute as desired when its code’s syntax is improper.

Objects

Regular Expressions are objects in both JavaScript and VBScript. These are Objects of the scripts. Scripts are the sequence of codes that are written in a specific order. Objects in its broadest sense are what you recognize. Objects can be a letter, a paragraph, or a page you read. It can be an image you see or a sound you hear. Even lines can be objects. The tangibles of the Computer Object just go on and on. For the programmer the word, objects, has special meaning. Regular Expression objects organize a collection of code where methods and properties can be attached.

Methods

Methods are basic procedures that are routinely run by the computer. For example, methods prescribe writing to the screen, printer, desktop or to a specific file. Properties are modifiers. They are used to store the objects attributes which are characteristics of the specified object. The wide range of characteristics can include: its color, font style, length, height, case or any other specific pattern requested. Another example is the global property which is all the occurrences of certain letters or words.

Regular Expression Methods

Regular Expressions have 3 methods: compile, execute, and test. Parentheses are placed after these methods, for example exec(), to indicate it is a procedure ready to perform action. The function compile(), is used to compile, put together an expression when the script executes. The exec() method checks for a match and returns the match; otherwise, if there is no match, it will return the absence of a value called null. The test() method is a Boolean test. If the match is there, it evaluates to true.

Results

Through the use of a combination of methods and properties, computer routines are developed and executed to provide specific functionality. For example, such functionality may direct the computer to write and provide the time and date. Regular Expressions are the modules of code that determine patterns. Specifically, this program object recognizes the occurrences of letters, words, numbers and symbols. In addition to pattern matching, Regular Expressions can be used for search and replace functions on text. In general, Regular Expressions provide for a reliable sample of traits or other observable features.

Two Examples
<script type="text/javascript">
var str= ("Is this a Regular Expression?");
var patt1=/[i-t]/g;
document.write (str.match(patt1));
</script>
<script type="text/javascript">
var str=("Rehobeth Beach, Dewey Beach, Whiskey Beach!");
var patt1=new RegExp("Beach", "g");
document.write (str.match(patt1));
</script>

The above script evaluates the pattern as follows:



The above script evaluates the pattern as follows:



Explanation:

  1. Script type is text/javasript.
  2. Some text is assigned to a variable, named str.
  3. Short hand is used to set the Regular Expression Object.
  4. This could have been written as:
  5. var patt1 = new RegExp("[i-t]","g");
  6. This is a global search meaning find all the characters that fit the pattern.
  7. The pattern is all letters from i through t.
  8. The Regular Expression object is assigned to a variable.
  9. The string match method is used to find all the matches in the attached string.
  10. Again, the pattern is specified through the Regular Expression.
  11. Finally, the document write method is called to display the results.

Explanation:

  1. Script type is text/javasript.
  2. Some text is assigned to a variable, named str.
  3. Next the Regular Expression Object is set with the pattern Beach.
  4. Two parameters, Beach & g, are passed to the Object.
  5. The Object is assigned to the variable, patt1.
  6. The "g" is the shorthand for the global property.
  7. This is a global search meaning find all the characters that fit the pattern.
  8. The pattern as indicated is Beach.
  9. The string match method is used to find all the matches in the attached string.
  10. Again, the pattern is specified through the Regular Expression.
  11. Finally, the document write method is called to display the results.

See W3Schools JavaScript RegExp [abc]

Guidance for Information:
Kingsley-Hughes, A., Kingsley-Hughes, K. & Reed, D. (2007).  VBScript.  

	Indianapolis, IN: Wiley Publishing, Inc.

World Wide Web Consortium [W3C] (1999-201). JavaScript RegExp Object. 
 
	Retrieved June 1, 2011 from: http://www.w3schools.com/js/js_obj_regexp.asp

Return to Table of Contents


DELAWARE'S MIDDLETOWN ODESSA AND TOWNSEND AREA